提交 5589c501 编写于 作者: chai2010's avatar chai2010

Merge branch 'master' of github.com:wa-lang/wa

...@@ -3,10 +3,9 @@ ...@@ -3,10 +3,9 @@
package compiler_wat package compiler_wat
import ( import (
"github.com/wa-lang/wa/internal/logger"
"github.com/wa-lang/wa/internal/ssa" "github.com/wa-lang/wa/internal/ssa"
) )
func (p *Compiler) compileConst(c *ssa.NamedConst) { func (p *Compiler) compileConst(c *ssa.NamedConst) {
logger.Fatal("Todo") //logger.Fatal("Todo")
} }
...@@ -444,6 +444,10 @@ func (g *functionGenerator) genCall(inst *ssa.Call) (insts []wat.Inst, ret_type ...@@ -444,6 +444,10 @@ func (g *functionGenerator) genCall(inst *ssa.Call) (insts []wat.Inst, ret_type
insts = append(insts, g.getValue(v).value.EmitPush()...) insts = append(insts, g.getValue(v).value.EmitPush()...)
} }
callee := inst.Call.StaticCallee() callee := inst.Call.StaticCallee()
if callee.Parent() != nil {
g.module.AddFunc(newFunctionGenerator(g.module).genFunction(callee))
}
if len(callee.LinkName()) > 0 { if len(callee.LinkName()) > 0 {
insts = append(insts, wat.NewInstCall(callee.LinkName())) insts = append(insts, wat.NewInstCall(callee.LinkName()))
} else { } else {
......
...@@ -272,8 +272,8 @@ func EmitGenIndexAddr(x, id Value) (insts []wat.Inst, ret_type ValueType) { ...@@ -272,8 +272,8 @@ func EmitGenIndexAddr(x, id Value) (insts []wat.Inst, ret_type ValueType) {
case *aSlice: case *aSlice:
base_type := x.Type().(Slice).Base base_type := x.Type().(Slice).Base
insts = append(insts, x.underlying.Extract("block").EmitPush()...) insts = append(insts, x.Extract("block").EmitPush()...)
insts = append(insts, x.underlying.Extract("data").EmitPush()...) insts = append(insts, x.Extract("data").EmitPush()...)
insts = append(insts, NewConst(strconv.Itoa(base_type.size()), I32{}).EmitPush()...) insts = append(insts, NewConst(strconv.Itoa(base_type.size()), I32{}).EmitPush()...)
insts = append(insts, id.EmitPush()...) insts = append(insts, id.EmitPush()...)
insts = append(insts, wat.NewInstMul(wat.I32{})) insts = append(insts, wat.NewInstMul(wat.I32{}))
...@@ -378,10 +378,10 @@ func EmitGenLen(x Value) (insts []wat.Inst) { ...@@ -378,10 +378,10 @@ func EmitGenLen(x Value) (insts []wat.Inst) {
insts = NewConst(strconv.Itoa(x.Type().(Array).Capacity), I32{}).EmitPush() insts = NewConst(strconv.Itoa(x.Type().(Array).Capacity), I32{}).EmitPush()
case *aSlice: case *aSlice:
insts = x.underlying.Extract("len").EmitPush() insts = x.Extract("len").EmitPush()
case *aString: case *aString:
insts = x.underlying.Extract("len").EmitPush() insts = x.Extract("len").EmitPush()
default: default:
logger.Fatalf("Todo: %T", x) logger.Fatalf("Todo: %T", x)
...@@ -393,8 +393,8 @@ func EmitGenLen(x Value) (insts []wat.Inst) { ...@@ -393,8 +393,8 @@ func EmitGenLen(x Value) (insts []wat.Inst) {
func EmitPrintString(v Value) (insts []wat.Inst) { func EmitPrintString(v Value) (insts []wat.Inst) {
s := v.(*aString) s := v.(*aString)
insts = append(insts, s.underlying.Extract("data").EmitPush()...) insts = append(insts, s.Extract("data").EmitPush()...)
insts = append(insts, s.underlying.Extract("len").EmitPush()...) insts = append(insts, s.Extract("len").EmitPush()...)
insts = append(insts, wat.NewInstCall("$runtime.waPuts")) insts = append(insts, wat.NewInstCall("$runtime.waPuts"))
return return
} }
......
...@@ -126,14 +126,14 @@ func (t Slice) genAppendFunc() string { ...@@ -126,14 +126,14 @@ func (t Slice) genAppendFunc() string {
f.Params = append(f.Params, y) f.Params = append(f.Params, y)
f.Results = append(f.Results, t) f.Results = append(f.Results, t)
x_len := NewLocal("x_len", x.underlying.Extract("len").Type()) x_len := NewLocal("x_len", x.Extract("len").Type())
f.Locals = append(f.Locals, x_len) f.Locals = append(f.Locals, x_len)
f.Insts = append(f.Insts, x.underlying.Extract("len").EmitPush()...) f.Insts = append(f.Insts, x.Extract("len").EmitPush()...)
f.Insts = append(f.Insts, x_len.EmitPop()...) f.Insts = append(f.Insts, x_len.EmitPop()...)
y_len := NewLocal("y_len", y.underlying.Extract("len").Type()) y_len := NewLocal("y_len", y.Extract("len").Type())
f.Locals = append(f.Locals, y_len) f.Locals = append(f.Locals, y_len)
f.Insts = append(f.Insts, y.underlying.Extract("len").EmitPush()...) f.Insts = append(f.Insts, y.Extract("len").EmitPush()...)
f.Insts = append(f.Insts, y_len.EmitPop()...) f.Insts = append(f.Insts, y_len.EmitPop()...)
//gen new_len: //gen new_len:
...@@ -146,7 +146,7 @@ func (t Slice) genAppendFunc() string { ...@@ -146,7 +146,7 @@ func (t Slice) genAppendFunc() string {
//if_new_len_le_cap //if_new_len_le_cap
f.Insts = append(f.Insts, new_len.EmitPush()...) f.Insts = append(f.Insts, new_len.EmitPush()...)
f.Insts = append(f.Insts, x.underlying.Extract("cap").EmitPush()...) f.Insts = append(f.Insts, x.Extract("cap").EmitPush()...)
f.Insts = append(f.Insts, wat.NewInstLe(wat.U32{})) f.Insts = append(f.Insts, wat.NewInstLe(wat.U32{}))
item := NewLocal("item", t.Base) item := NewLocal("item", t.Base)
...@@ -161,17 +161,17 @@ func (t Slice) genAppendFunc() string { ...@@ -161,17 +161,17 @@ func (t Slice) genAppendFunc() string {
{ //if_true { //if_true
var if_true []wat.Inst var if_true []wat.Inst
if_true = append(if_true, x.underlying.Extract("block").EmitPush()...) if_true = append(if_true, x.Extract("block").EmitPush()...)
if_true = append(if_true, x.underlying.Extract("data").EmitPush()...) if_true = append(if_true, x.Extract("data").EmitPush()...)
if_true = append(if_true, new_len.EmitPush()...) if_true = append(if_true, new_len.EmitPush()...)
if_true = append(if_true, x.underlying.Extract("cap").EmitPush()...) if_true = append(if_true, x.Extract("cap").EmitPush()...)
//get src //get src
if_true = append(if_true, y.underlying.Extract("data").EmitPush()...) if_true = append(if_true, y.Extract("data").EmitPush()...)
if_true = append(if_true, src.EmitPop()...) if_true = append(if_true, src.EmitPop()...)
//get dest //get dest
if_true = append(if_true, x.underlying.Extract("data").EmitPush()...) if_true = append(if_true, x.Extract("data").EmitPush()...)
if_true = append(if_true, item_size.EmitPush()...) if_true = append(if_true, item_size.EmitPush()...)
if_true = append(if_true, x_len.EmitPush()...) if_true = append(if_true, x_len.EmitPush()...)
if_true = append(if_true, wat.NewInstMul(wat.U32{})) if_true = append(if_true, wat.NewInstMul(wat.U32{}))
...@@ -235,7 +235,7 @@ func (t Slice) genAppendFunc() string { ...@@ -235,7 +235,7 @@ func (t Slice) genAppendFunc() string {
//x->new //x->new
{ {
if_false = append(if_false, x.underlying.Extract("data").EmitPush()...) if_false = append(if_false, x.Extract("data").EmitPush()...)
if_false = append(if_false, src.EmitPop()...) if_false = append(if_false, src.EmitPop()...)
block := wat.NewInstBlock("block2") block := wat.NewInstBlock("block2")
...@@ -273,7 +273,7 @@ func (t Slice) genAppendFunc() string { ...@@ -273,7 +273,7 @@ func (t Slice) genAppendFunc() string {
//y->new //y->new
{ {
if_false = append(if_false, y.underlying.Extract("data").EmitPush()...) if_false = append(if_false, y.Extract("data").EmitPush()...)
if_false = append(if_false, src.EmitPop()...) if_false = append(if_false, src.EmitPop()...)
block := wat.NewInstBlock("block3") block := wat.NewInstBlock("block3")
...@@ -324,37 +324,38 @@ func (t Slice) genAppendFunc() string { ...@@ -324,37 +324,38 @@ func (t Slice) genAppendFunc() string {
aSlice: aSlice:
**************************************/ **************************************/
type aSlice struct { type aSlice struct {
aValue aStruct
underlying aStruct typ Slice
} }
func newValueSlice(name string, kind ValueKind, base_type ValueType) *aSlice { func newValueSlice(name string, kind ValueKind, base_type ValueType) *aSlice {
var v aSlice var v aSlice
slice_type := NewSlice(base_type) v.typ = NewSlice(base_type)
v.aValue = aValue{name: name, kind: kind, typ: slice_type} v.aStruct = *newValueStruct(name, kind, v.typ.Struct)
v.underlying = *newValueStruct(name, kind, slice_type.Struct)
return &v return &v
} }
func (v *aSlice) raw() []wat.Value { return v.underlying.raw() } func (v *aSlice) Type() ValueType { return v.typ }
func (v *aSlice) EmitInit() []wat.Inst { return v.underlying.EmitInit() }
func (v *aSlice) EmitPush() []wat.Inst { return v.underlying.EmitPush() } func (v *aSlice) raw() []wat.Value { return v.aStruct.raw() }
func (v *aSlice) EmitPop() []wat.Inst { return v.underlying.EmitPop() } func (v *aSlice) EmitInit() []wat.Inst { return v.aStruct.EmitInit() }
func (v *aSlice) EmitRelease() []wat.Inst { return v.underlying.EmitRelease() } func (v *aSlice) EmitPush() []wat.Inst { return v.aStruct.EmitPush() }
func (v *aSlice) EmitPop() []wat.Inst { return v.aStruct.EmitPop() }
func (v *aSlice) EmitRelease() []wat.Inst { return v.aStruct.EmitRelease() }
func (v *aSlice) emitStoreToAddr(addr Value, offset int) []wat.Inst { func (v *aSlice) emitStoreToAddr(addr Value, offset int) []wat.Inst {
return v.underlying.emitStoreToAddr(addr, offset) return v.aStruct.emitStoreToAddr(addr, offset)
} }
func (v *aSlice) emitSub(low, high Value) (insts []wat.Inst) { func (v *aSlice) emitSub(low, high Value) (insts []wat.Inst) {
//block //block
insts = append(insts, v.underlying.Extract("block").EmitPush()...) insts = append(insts, v.Extract("block").EmitPush()...)
//data: //data:
if low == nil { if low == nil {
low = NewConst("0", U32{}) low = NewConst("0", U32{})
} }
insts = append(insts, v.underlying.Extract("data").EmitPush()...) insts = append(insts, v.Extract("data").EmitPush()...)
insts = append(insts, NewConst(strconv.Itoa(v.Type().(Slice).Base.size()), U32{}).EmitPush()...) insts = append(insts, NewConst(strconv.Itoa(v.Type().(Slice).Base.size()), U32{}).EmitPush()...)
insts = append(insts, low.EmitPush()...) insts = append(insts, low.EmitPush()...)
insts = append(insts, wat.NewInstMul(wat.U32{})) insts = append(insts, wat.NewInstMul(wat.U32{}))
...@@ -362,14 +363,14 @@ func (v *aSlice) emitSub(low, high Value) (insts []wat.Inst) { ...@@ -362,14 +363,14 @@ func (v *aSlice) emitSub(low, high Value) (insts []wat.Inst) {
//len: //len:
if high == nil { if high == nil {
high = v.underlying.Extract("len") high = v.Extract("len")
} }
insts = append(insts, high.EmitPush()...) insts = append(insts, high.EmitPush()...)
insts = append(insts, low.EmitPush()...) insts = append(insts, low.EmitPush()...)
insts = append(insts, wat.NewInstSub(wat.U32{})) insts = append(insts, wat.NewInstSub(wat.U32{}))
//cap: //cap:
insts = append(insts, v.underlying.Extract("cap").EmitPush()...) insts = append(insts, v.Extract("cap").EmitPush()...)
insts = append(insts, low.EmitPush()...) insts = append(insts, low.EmitPush()...)
insts = append(insts, wat.NewInstSub(wat.U32{})) insts = append(insts, wat.NewInstSub(wat.U32{}))
......
...@@ -49,14 +49,14 @@ func (t String) genAppendStrFunc() string { ...@@ -49,14 +49,14 @@ func (t String) genAppendStrFunc() string {
f.Params = append(f.Params, y) f.Params = append(f.Params, y)
f.Results = append(f.Results, t) f.Results = append(f.Results, t)
x_len := NewLocal("x_len", x.underlying.Extract("len").Type()) x_len := NewLocal("x_len", x.Extract("len").Type())
f.Locals = append(f.Locals, x_len) f.Locals = append(f.Locals, x_len)
f.Insts = append(f.Insts, x.underlying.Extract("len").EmitPush()...) f.Insts = append(f.Insts, x.Extract("len").EmitPush()...)
f.Insts = append(f.Insts, x_len.EmitPop()...) f.Insts = append(f.Insts, x_len.EmitPop()...)
y_len := NewLocal("y_len", y.underlying.Extract("len").Type()) y_len := NewLocal("y_len", y.Extract("len").Type())
f.Locals = append(f.Locals, y_len) f.Locals = append(f.Locals, y_len)
f.Insts = append(f.Insts, y.underlying.Extract("len").EmitPush()...) f.Insts = append(f.Insts, y.Extract("len").EmitPush()...)
f.Insts = append(f.Insts, y_len.EmitPop()...) f.Insts = append(f.Insts, y_len.EmitPop()...)
//gen new_len: //gen new_len:
...@@ -88,7 +88,7 @@ func (t String) genAppendStrFunc() string { ...@@ -88,7 +88,7 @@ func (t String) genAppendStrFunc() string {
//x->new //x->new
{ {
f.Insts = append(f.Insts, x.underlying.Extract("data").EmitPush()...) f.Insts = append(f.Insts, x.Extract("data").EmitPush()...)
f.Insts = append(f.Insts, src.EmitPop()...) f.Insts = append(f.Insts, src.EmitPop()...)
block := wat.NewInstBlock("block2") block := wat.NewInstBlock("block2")
...@@ -126,7 +126,7 @@ func (t String) genAppendStrFunc() string { ...@@ -126,7 +126,7 @@ func (t String) genAppendStrFunc() string {
//y->new //y->new
{ {
f.Insts = append(f.Insts, y.underlying.Extract("data").EmitPush()...) f.Insts = append(f.Insts, y.Extract("data").EmitPush()...)
f.Insts = append(f.Insts, src.EmitPop()...) f.Insts = append(f.Insts, src.EmitPop()...)
block := wat.NewInstBlock("block3") block := wat.NewInstBlock("block3")
...@@ -174,8 +174,8 @@ func (t String) genAppendStrFunc() string { ...@@ -174,8 +174,8 @@ func (t String) genAppendStrFunc() string {
aString: aString:
**************************************/ **************************************/
type aString struct { type aString struct {
aValue aStruct
underlying aStruct typ String
} }
func newValueString(name string, kind ValueKind) *aString { func newValueString(name string, kind ValueKind) *aString {
...@@ -187,36 +187,38 @@ func newValueString(name string, kind ValueKind) *aString { ...@@ -187,36 +187,38 @@ func newValueString(name string, kind ValueKind) *aString {
string_type.findFieldByName("data").const_val = NewConst(strconv.Itoa(ptr), NewPointer(U8{})) string_type.findFieldByName("data").const_val = NewConst(strconv.Itoa(ptr), NewPointer(U8{}))
string_type.findFieldByName("len").const_val = NewConst(strconv.Itoa(len(name)), U32{}) string_type.findFieldByName("len").const_val = NewConst(strconv.Itoa(len(name)), U32{})
} }
v.aValue = aValue{name: name, kind: kind, typ: string_type} v.typ = string_type
v.underlying = *newValueStruct(name, kind, string_type.Struct) v.aStruct = *newValueStruct(name, kind, string_type.Struct)
return &v return &v
} }
func (v *aString) raw() []wat.Value { return v.underlying.raw() } func (v *aString) Type() ValueType { return v.typ }
func (v *aString) EmitInit() []wat.Inst { return v.underlying.EmitInit() }
func (v *aString) EmitPush() []wat.Inst { return v.underlying.EmitPush() } func (v *aString) raw() []wat.Value { return v.aStruct.raw() }
func (v *aString) EmitPop() []wat.Inst { return v.underlying.EmitPop() } func (v *aString) EmitInit() []wat.Inst { return v.aStruct.EmitInit() }
func (v *aString) EmitRelease() []wat.Inst { return v.underlying.EmitRelease() } func (v *aString) EmitPush() []wat.Inst { return v.aStruct.EmitPush() }
func (v *aString) EmitPop() []wat.Inst { return v.aStruct.EmitPop() }
func (v *aString) EmitRelease() []wat.Inst { return v.aStruct.EmitRelease() }
func (v *aString) emitStoreToAddr(addr Value, offset int) []wat.Inst { func (v *aString) emitStoreToAddr(addr Value, offset int) []wat.Inst {
return v.underlying.emitStoreToAddr(addr, offset) return v.aStruct.emitStoreToAddr(addr, offset)
} }
func (v *aString) emitSub(low, high Value) (insts []wat.Inst) { func (v *aString) emitSub(low, high Value) (insts []wat.Inst) {
//block //block
insts = append(insts, v.underlying.Extract("block").EmitPush()...) insts = append(insts, v.Extract("block").EmitPush()...)
//data: //data:
if low == nil { if low == nil {
low = NewConst("0", U32{}) low = NewConst("0", U32{})
} }
insts = append(insts, v.underlying.Extract("data").EmitPush()...) insts = append(insts, v.Extract("data").EmitPush()...)
insts = append(insts, low.EmitPush()...) insts = append(insts, low.EmitPush()...)
insts = append(insts, wat.NewInstAdd(wat.U32{})) insts = append(insts, wat.NewInstAdd(wat.U32{}))
//len: //len:
if high == nil { if high == nil {
high = v.underlying.Extract("len") high = v.Extract("len")
} }
insts = append(insts, high.EmitPush()...) insts = append(insts, high.EmitPush()...)
insts = append(insts, low.EmitPush()...) insts = append(insts, low.EmitPush()...)
...@@ -226,7 +228,7 @@ func (v *aString) emitSub(low, high Value) (insts []wat.Inst) { ...@@ -226,7 +228,7 @@ func (v *aString) emitSub(low, high Value) (insts []wat.Inst) {
} }
func (v *aString) emitAt(index Value) (insts []wat.Inst) { func (v *aString) emitAt(index Value) (insts []wat.Inst) {
insts = append(insts, v.underlying.Extract("data").EmitPush()...) insts = append(insts, v.Extract("data").EmitPush()...)
insts = append(insts, index.EmitPush()...) insts = append(insts, index.EmitPush()...)
insts = append(insts, wat.NewInstAdd(wat.I32{})) insts = append(insts, wat.NewInstAdd(wat.I32{}))
insts = append(insts, wat.NewInstLoad8u(0, 1)) insts = append(insts, wat.NewInstLoad8u(0, 1))
...@@ -234,13 +236,13 @@ func (v *aString) emitAt(index Value) (insts []wat.Inst) { ...@@ -234,13 +236,13 @@ func (v *aString) emitAt(index Value) (insts []wat.Inst) {
} }
func (v *aString) emitAt_CommaOk(index Value) (insts []wat.Inst) { func (v *aString) emitAt_CommaOk(index Value) (insts []wat.Inst) {
insts = append(insts, v.underlying.Extract("len").EmitPush()...) insts = append(insts, v.Extract("len").EmitPush()...)
insts = append(insts, index.EmitPush()...) insts = append(insts, index.EmitPush()...)
insts = append(insts, wat.NewInstGt(wat.U32{})) insts = append(insts, wat.NewInstGt(wat.U32{}))
{ {
var instsTrue, instsFalse []wat.Inst var instsTrue, instsFalse []wat.Inst
instsTrue = append(instsTrue, v.underlying.Extract("data").EmitPush()...) instsTrue = append(instsTrue, v.Extract("data").EmitPush()...)
instsTrue = append(instsTrue, index.EmitPush()...) instsTrue = append(instsTrue, index.EmitPush()...)
instsTrue = append(instsTrue, wat.NewInstAdd(wat.I32{})) instsTrue = append(instsTrue, wat.NewInstAdd(wat.I32{}))
instsTrue = append(instsTrue, wat.NewInstLoad8u(0, 1)) instsTrue = append(instsTrue, wat.NewInstLoad8u(0, 1))
......
...@@ -46,25 +46,27 @@ func (t Tuple) EmitLoadFromAddr(addr Value, offset int) []wat.Inst { ...@@ -46,25 +46,27 @@ func (t Tuple) EmitLoadFromAddr(addr Value, offset int) []wat.Inst {
aTuple: aTuple:
**************************************/ **************************************/
type aTuple struct { type aTuple struct {
aValue aStruct
underlying aStruct typ Tuple
} }
func newValueTuple(name string, kind ValueKind, typ Tuple) *aTuple { func newValueTuple(name string, kind ValueKind, typ Tuple) *aTuple {
var v aTuple var v aTuple
v.aValue = aValue{name: name, kind: kind, typ: typ} v.typ = typ
v.underlying = *newValueStruct(name, kind, typ.Struct) v.aStruct = *newValueStruct(name, kind, typ.Struct)
return &v return &v
} }
func (v *aTuple) raw() []wat.Value { return v.underlying.raw() } func (v *aTuple) Type() ValueType { return v.typ }
func (v *aTuple) EmitInit() []wat.Inst { return v.underlying.EmitInit() }
func (v *aTuple) EmitPush() []wat.Inst { return v.underlying.EmitPush() } func (v *aTuple) raw() []wat.Value { return v.aStruct.raw() }
func (v *aTuple) EmitPop() []wat.Inst { return v.underlying.EmitPop() } func (v *aTuple) EmitInit() []wat.Inst { return v.aStruct.EmitInit() }
func (v *aTuple) EmitRelease() []wat.Inst { return v.underlying.EmitRelease() } func (v *aTuple) EmitPush() []wat.Inst { return v.aStruct.EmitPush() }
func (v *aTuple) EmitPop() []wat.Inst { return v.aStruct.EmitPop() }
func (v *aTuple) EmitRelease() []wat.Inst { return v.aStruct.EmitRelease() }
func (v *aTuple) emitStoreToAddr(addr Value, offset int) []wat.Inst { func (v *aTuple) emitStoreToAddr(addr Value, offset int) []wat.Inst {
return v.underlying.emitStoreToAddr(addr, offset) return v.aStruct.emitStoreToAddr(addr, offset)
} }
func (v *aTuple) Extract(id int) Value { func (v *aTuple) Extract(id int) Value {
...@@ -73,5 +75,5 @@ func (v *aTuple) Extract(id int) Value { ...@@ -73,5 +75,5 @@ func (v *aTuple) Extract(id int) Value {
panic("id >= len(st.Members)") panic("id >= len(st.Members)")
} }
return v.underlying.genSubValue(st.Members[id]) return v.genSubValue(st.Members[id])
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册