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

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

......@@ -3,10 +3,9 @@
package compiler_wat
import (
"github.com/wa-lang/wa/internal/logger"
"github.com/wa-lang/wa/internal/ssa"
)
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
insts = append(insts, g.getValue(v).value.EmitPush()...)
}
callee := inst.Call.StaticCallee()
if callee.Parent() != nil {
g.module.AddFunc(newFunctionGenerator(g.module).genFunction(callee))
}
if len(callee.LinkName()) > 0 {
insts = append(insts, wat.NewInstCall(callee.LinkName()))
} else {
......
......@@ -272,8 +272,8 @@ func EmitGenIndexAddr(x, id Value) (insts []wat.Inst, ret_type ValueType) {
case *aSlice:
base_type := x.Type().(Slice).Base
insts = append(insts, x.underlying.Extract("block").EmitPush()...)
insts = append(insts, x.underlying.Extract("data").EmitPush()...)
insts = append(insts, x.Extract("block").EmitPush()...)
insts = append(insts, x.Extract("data").EmitPush()...)
insts = append(insts, NewConst(strconv.Itoa(base_type.size()), I32{}).EmitPush()...)
insts = append(insts, id.EmitPush()...)
insts = append(insts, wat.NewInstMul(wat.I32{}))
......@@ -378,10 +378,10 @@ func EmitGenLen(x Value) (insts []wat.Inst) {
insts = NewConst(strconv.Itoa(x.Type().(Array).Capacity), I32{}).EmitPush()
case *aSlice:
insts = x.underlying.Extract("len").EmitPush()
insts = x.Extract("len").EmitPush()
case *aString:
insts = x.underlying.Extract("len").EmitPush()
insts = x.Extract("len").EmitPush()
default:
logger.Fatalf("Todo: %T", x)
......@@ -393,8 +393,8 @@ func EmitGenLen(x Value) (insts []wat.Inst) {
func EmitPrintString(v Value) (insts []wat.Inst) {
s := v.(*aString)
insts = append(insts, s.underlying.Extract("data").EmitPush()...)
insts = append(insts, s.underlying.Extract("len").EmitPush()...)
insts = append(insts, s.Extract("data").EmitPush()...)
insts = append(insts, s.Extract("len").EmitPush()...)
insts = append(insts, wat.NewInstCall("$runtime.waPuts"))
return
}
......
......@@ -126,14 +126,14 @@ func (t Slice) genAppendFunc() string {
f.Params = append(f.Params, y)
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.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()...)
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.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()...)
//gen new_len:
......@@ -146,7 +146,7 @@ func (t Slice) genAppendFunc() string {
//if_new_len_le_cap
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{}))
item := NewLocal("item", t.Base)
......@@ -161,17 +161,17 @@ func (t Slice) genAppendFunc() string {
{ //if_true
var if_true []wat.Inst
if_true = append(if_true, x.underlying.Extract("block").EmitPush()...)
if_true = append(if_true, x.underlying.Extract("data").EmitPush()...)
if_true = append(if_true, x.Extract("block").EmitPush()...)
if_true = append(if_true, x.Extract("data").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
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()...)
//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, x_len.EmitPush()...)
if_true = append(if_true, wat.NewInstMul(wat.U32{}))
......@@ -235,7 +235,7 @@ func (t Slice) genAppendFunc() string {
//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()...)
block := wat.NewInstBlock("block2")
......@@ -273,7 +273,7 @@ func (t Slice) genAppendFunc() string {
//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()...)
block := wat.NewInstBlock("block3")
......@@ -324,37 +324,38 @@ func (t Slice) genAppendFunc() string {
aSlice:
**************************************/
type aSlice struct {
aValue
underlying aStruct
aStruct
typ Slice
}
func newValueSlice(name string, kind ValueKind, base_type ValueType) *aSlice {
var v aSlice
slice_type := NewSlice(base_type)
v.aValue = aValue{name: name, kind: kind, typ: slice_type}
v.underlying = *newValueStruct(name, kind, slice_type.Struct)
v.typ = NewSlice(base_type)
v.aStruct = *newValueStruct(name, kind, v.typ.Struct)
return &v
}
func (v *aSlice) raw() []wat.Value { return v.underlying.raw() }
func (v *aSlice) EmitInit() []wat.Inst { return v.underlying.EmitInit() }
func (v *aSlice) EmitPush() []wat.Inst { return v.underlying.EmitPush() }
func (v *aSlice) EmitPop() []wat.Inst { return v.underlying.EmitPop() }
func (v *aSlice) EmitRelease() []wat.Inst { return v.underlying.EmitRelease() }
func (v *aSlice) Type() ValueType { return v.typ }
func (v *aSlice) raw() []wat.Value { return v.aStruct.raw() }
func (v *aSlice) EmitInit() []wat.Inst { return v.aStruct.EmitInit() }
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 {
return v.underlying.emitStoreToAddr(addr, offset)
return v.aStruct.emitStoreToAddr(addr, offset)
}
func (v *aSlice) emitSub(low, high Value) (insts []wat.Inst) {
//block
insts = append(insts, v.underlying.Extract("block").EmitPush()...)
insts = append(insts, v.Extract("block").EmitPush()...)
//data:
if low == nil {
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, low.EmitPush()...)
insts = append(insts, wat.NewInstMul(wat.U32{}))
......@@ -362,14 +363,14 @@ func (v *aSlice) emitSub(low, high Value) (insts []wat.Inst) {
//len:
if high == nil {
high = v.underlying.Extract("len")
high = v.Extract("len")
}
insts = append(insts, high.EmitPush()...)
insts = append(insts, low.EmitPush()...)
insts = append(insts, wat.NewInstSub(wat.U32{}))
//cap:
insts = append(insts, v.underlying.Extract("cap").EmitPush()...)
insts = append(insts, v.Extract("cap").EmitPush()...)
insts = append(insts, low.EmitPush()...)
insts = append(insts, wat.NewInstSub(wat.U32{}))
......
......@@ -49,14 +49,14 @@ func (t String) genAppendStrFunc() string {
f.Params = append(f.Params, y)
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.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()...)
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.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()...)
//gen new_len:
......@@ -88,7 +88,7 @@ func (t String) genAppendStrFunc() string {
//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()...)
block := wat.NewInstBlock("block2")
......@@ -126,7 +126,7 @@ func (t String) genAppendStrFunc() string {
//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()...)
block := wat.NewInstBlock("block3")
......@@ -174,8 +174,8 @@ func (t String) genAppendStrFunc() string {
aString:
**************************************/
type aString struct {
aValue
underlying aStruct
aStruct
typ String
}
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("len").const_val = NewConst(strconv.Itoa(len(name)), U32{})
}
v.aValue = aValue{name: name, kind: kind, typ: string_type}
v.underlying = *newValueStruct(name, kind, string_type.Struct)
v.typ = string_type
v.aStruct = *newValueStruct(name, kind, string_type.Struct)
return &v
}
func (v *aString) raw() []wat.Value { return v.underlying.raw() }
func (v *aString) EmitInit() []wat.Inst { return v.underlying.EmitInit() }
func (v *aString) EmitPush() []wat.Inst { return v.underlying.EmitPush() }
func (v *aString) EmitPop() []wat.Inst { return v.underlying.EmitPop() }
func (v *aString) EmitRelease() []wat.Inst { return v.underlying.EmitRelease() }
func (v *aString) Type() ValueType { return v.typ }
func (v *aString) raw() []wat.Value { return v.aStruct.raw() }
func (v *aString) EmitInit() []wat.Inst { return v.aStruct.EmitInit() }
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 {
return v.underlying.emitStoreToAddr(addr, offset)
return v.aStruct.emitStoreToAddr(addr, offset)
}
func (v *aString) emitSub(low, high Value) (insts []wat.Inst) {
//block
insts = append(insts, v.underlying.Extract("block").EmitPush()...)
insts = append(insts, v.Extract("block").EmitPush()...)
//data:
if low == nil {
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, wat.NewInstAdd(wat.U32{}))
//len:
if high == nil {
high = v.underlying.Extract("len")
high = v.Extract("len")
}
insts = append(insts, high.EmitPush()...)
insts = append(insts, low.EmitPush()...)
......@@ -226,7 +228,7 @@ func (v *aString) emitSub(low, high 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, wat.NewInstAdd(wat.I32{}))
insts = append(insts, wat.NewInstLoad8u(0, 1))
......@@ -234,13 +236,13 @@ func (v *aString) emitAt(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, wat.NewInstGt(wat.U32{}))
{
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, wat.NewInstAdd(wat.I32{}))
instsTrue = append(instsTrue, wat.NewInstLoad8u(0, 1))
......
......@@ -46,25 +46,27 @@ func (t Tuple) EmitLoadFromAddr(addr Value, offset int) []wat.Inst {
aTuple:
**************************************/
type aTuple struct {
aValue
underlying aStruct
aStruct
typ Tuple
}
func newValueTuple(name string, kind ValueKind, typ Tuple) *aTuple {
var v aTuple
v.aValue = aValue{name: name, kind: kind, typ: typ}
v.underlying = *newValueStruct(name, kind, typ.Struct)
v.typ = typ
v.aStruct = *newValueStruct(name, kind, typ.Struct)
return &v
}
func (v *aTuple) raw() []wat.Value { return v.underlying.raw() }
func (v *aTuple) EmitInit() []wat.Inst { return v.underlying.EmitInit() }
func (v *aTuple) EmitPush() []wat.Inst { return v.underlying.EmitPush() }
func (v *aTuple) EmitPop() []wat.Inst { return v.underlying.EmitPop() }
func (v *aTuple) EmitRelease() []wat.Inst { return v.underlying.EmitRelease() }
func (v *aTuple) Type() ValueType { return v.typ }
func (v *aTuple) raw() []wat.Value { return v.aStruct.raw() }
func (v *aTuple) EmitInit() []wat.Inst { return v.aStruct.EmitInit() }
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 {
return v.underlying.emitStoreToAddr(addr, offset)
return v.aStruct.emitStoreToAddr(addr, offset)
}
func (v *aTuple) Extract(id int) Value {
......@@ -73,5 +75,5 @@ func (v *aTuple) Extract(id int) Value {
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.
先完成此消息的编辑!
想要评论请 注册