提交 7a06fb52 编写于 作者: 3 3dgen

wir 增加原生 bool 类型

上级 bd7f9f25
......@@ -63,9 +63,9 @@ func (g *functionGenerator) getValue(i ssa.Value) valueWrap {
case types.Bool, types.UntypedBool:
if constant.BoolVal(v.Value) {
return valueWrap{value: wir.NewConst("1", g.module.I32)}
return valueWrap{value: wir.NewConst("1", g.module.BOOL)}
} else {
return valueWrap{value: wir.NewConst("0", g.module.I32)}
return valueWrap{value: wir.NewConst("0", g.module.BOOL)}
}
case types.Uint8:
......@@ -628,7 +628,10 @@ func (g *functionGenerator) genBuiltin(call *ssa.CallCommon) (insts []wat.Inst,
insts = append(insts, wat.NewInstCall("$runtime.waPrintRune"))
}
if avt.Equal(g.module.I32) {
if avt.Equal(g.module.BOOL) {
insts = append(insts, av.EmitPush()...)
insts = append(insts, wat.NewInstCall("$runtime.waPrintBool"))
} else if avt.Equal(g.module.I32) {
insts = append(insts, av.EmitPush()...)
insts = append(insts, wat.NewInstCall("$runtime.waPrintI32"))
} else if avt.Equal(g.module.U8) || avt.Equal(g.module.U16) || avt.Equal(g.module.U32) {
......@@ -769,8 +772,8 @@ func (g *functionGenerator) genStore(inst *ssa.Store) []wat.Inst {
func (g *functionGenerator) genIf(inst *ssa.If) []wat.Inst {
cond := g.getValue(inst.Cond)
if !cond.value.Type().Equal(g.module.I32) {
logger.Fatal("cond.type() != i32")
if !cond.value.Type().Equal(g.module.BOOL) {
logger.Fatal("cond.type() != bool")
}
insts := cond.value.EmitPush()
......
......@@ -49,7 +49,10 @@ func (tLib *typeLib) compile(from types.Type) wir.ValueType {
switch t := from.(type) {
case *types.Basic:
switch t.Kind() {
case types.Bool, types.UntypedBool, types.Int, types.UntypedInt:
case types.Bool, types.UntypedBool:
newType = tLib.module.BOOL
case types.Int, types.UntypedInt:
newType = tLib.module.I32
case types.Int32:
......@@ -173,6 +176,12 @@ func (tLib *typeLib) compile(from types.Type) wir.ValueType {
newType = tLib.module.GenValueType_Dup(pkg_name+"."+obj_name, tLib.compile(ut))
default:
//pkg_name := ""
//if t.Obj().Pkg() != nil {
// pkg_name, _ = wir.GetPkgMangleName(t.Obj().Pkg().Path())
//}
//obj_name := wir.GenSymbolName(t.Obj().Name())
//newType = tLib.module.GenValueType_Dup(pkg_name+"."+obj_name, tLib.compile(ut))
logger.Fatalf("Todo:%T", ut)
}
......
......@@ -42,11 +42,25 @@ func (m *Module) EmitUnOp(x Value, op wat.OpCode) (insts []wat.Inst, ret_type Va
insts = append(insts, NewConst("0", ret_type).EmitPush()...)
insts = append(insts, x.EmitPush()...)
insts = append(insts, wat.NewInstSub(toWatType(ret_type)))
if ret_type.Equal(m.U8) {
insts = append(insts, wat.NewInstConst(wat.I32{}, "255"))
insts = append(insts, wat.NewInstAnd(wat.I32{}))
} else if ret_type.Equal(m.U16) {
insts = append(insts, wat.NewInstConst(wat.I32{}, "65535"))
insts = append(insts, wat.NewInstAnd(wat.I32{}))
}
case wat.OpCodeXor:
insts = append(insts, NewConst("-1", ret_type).EmitPush()...)
insts = append(insts, x.EmitPush()...)
insts = append(insts, wat.NewInstXor(toWatType(ret_type)))
if ret_type.Equal(m.U8) {
insts = append(insts, wat.NewInstConst(wat.I32{}, "255"))
insts = append(insts, wat.NewInstAnd(wat.I32{}))
} else if ret_type.Equal(m.U16) {
insts = append(insts, wat.NewInstConst(wat.I32{}, "65535"))
insts = append(insts, wat.NewInstAnd(wat.I32{}))
}
case wat.OpCodeNot:
insts = append(insts, x.EmitPush()...)
......@@ -56,6 +70,7 @@ func (m *Module) EmitUnOp(x Value, op wat.OpCode) (insts []wat.Inst, ret_type Va
logger.Fatalf("Todo: %[1]v: %[1]T", op)
}
return
}
......@@ -138,37 +153,37 @@ func (m *Module) EmitBinOp(x, y Value, op wat.OpCode) (insts []wat.Inst, ret_typ
case wat.OpCodeEql:
ins, _ := x.emitEq(y)
insts = append(insts, ins...)
ret_type = m.I32
ret_type = m.BOOL
case wat.OpCodeNe:
ins, _ := x.emitEq(y)
insts = append(insts, ins...)
insts = append(insts, wat.NewInstEqz(wat.I32{}))
ret_type = m.I32
ret_type = m.BOOL
case wat.OpCodeLt:
insts = append(insts, x.EmitPush()...)
insts = append(insts, y.EmitPush()...)
insts = append(insts, wat.NewInstLt(toWatType(x.Type())))
ret_type = m.I32
ret_type = m.BOOL
case wat.OpCodeGt:
insts = append(insts, x.EmitPush()...)
insts = append(insts, y.EmitPush()...)
insts = append(insts, wat.NewInstGt(toWatType(x.Type())))
ret_type = m.I32
ret_type = m.BOOL
case wat.OpCodeLe:
insts = append(insts, x.EmitPush()...)
insts = append(insts, y.EmitPush()...)
insts = append(insts, wat.NewInstLe(toWatType(x.Type())))
ret_type = m.I32
ret_type = m.BOOL
case wat.OpCodeGe:
insts = append(insts, x.EmitPush()...)
insts = append(insts, y.EmitPush()...)
insts = append(insts, wat.NewInstGe(toWatType(x.Type())))
ret_type = m.I32
ret_type = m.BOOL
case wat.OpCodeAnd:
ret_type = x.Type()
......
......@@ -18,7 +18,7 @@ type fnSigWrap struct {
Module:
**************************************/
type Module struct {
VOID, RUNE, U8, U16, I32, U32, UPTR, I64, U64, F32, F64, STRING, BYTES ValueType
VOID, BOOL, RUNE, U8, U16, I32, U32, UPTR, I64, U64, F32, F64, STRING, BYTES ValueType
types_map map[string]ValueType
usedConcreteTypes []ValueType
......@@ -53,6 +53,7 @@ func NewModule() *Module {
var m Module
m.VOID = &tVoid{}
m.BOOL = &tBool{}
m.RUNE = &tRune{}
//m.I8 = &tI8{}
m.U8 = &tU8{}
......@@ -68,6 +69,7 @@ func NewModule() *Module {
m.types_map = make(map[string]ValueType)
m.addValueType(m.VOID)
m.addValueType(m.BOOL)
m.addValueType(m.RUNE)
//m.addValueType(m.I8)
m.addValueType(m.U8)
......
......@@ -27,7 +27,7 @@ func NewGlobal(name string, typ ValueType, as_pointer bool) Value {
func newValue(name string, kind ValueKind, typ ValueType) Value {
switch typ := typ.(type) {
case *tI8, *tU8, *tI16, *tU16, *tI32, *tU32, *tI64, *tU64, *tF32, *tF64, *tRune:
case *tI8, *tU8, *tI16, *tU16, *tI32, *tU32, *tI64, *tU64, *tF32, *tF64, *tRune, *tBool:
return newValue_Basic(name, kind, typ)
case *Ptr:
......
......@@ -12,6 +12,7 @@ type TypeKind uint8
const (
kUnknown TypeKind = iota
kVoid
kBool
kU8
kU16
kU32
......@@ -41,7 +42,7 @@ func toWatType(t ValueType) wat.ValueType {
case *tI32, *tRune: //Todo: *tI8, *tI16*
return wat.I32{}
case *tU32, *tU8, *tU16:
case *tU32, *tU8, *tU16, *tBool:
return wat.U32{}
case *tI64:
......@@ -119,6 +120,30 @@ func (t *tVoid) EmitLoadFromAddr(addr Value, offset int) []wat.Inst {
return nil
}
/**************************************
tBool:
**************************************/
type tBool struct {
tCommon
}
func (t *tBool) Name() string { return "bool" }
func (t *tBool) Size() int { return 1 }
func (t *tBool) align() int { return 1 }
func (t *tBool) Kind() TypeKind { return kBool }
func (t *tBool) onFree() int { return 0 }
func (t *tBool) Raw() []wat.ValueType { return []wat.ValueType{wat.I32{}} }
func (t *tBool) Equal(u ValueType) bool { _, ok := u.(*tBool); return ok }
func (t *tBool) EmitLoadFromAddr(addr Value, offset int) []wat.Inst {
//if !addr.Type().(*Ptr).Base.Equal(t) {
// logger.Fatal("Type not match")
// return nil
//}
insts := addr.EmitPush()
insts = append(insts, wat.NewInstLoad(toWatType(t), offset, 1))
return insts
}
/**************************************
tRune:
**************************************/
......
......@@ -15,6 +15,16 @@ import (
func MvpInstantiate(ctx context.Context, rt wazero.Runtime) (api.Closer, error) {
return rt.NewHostModuleBuilder(config.WaOS_mvp).
// func waPrintBool(v: bool)
NewFunctionBuilder().
WithFunc(func(ctx context.Context, m api.Module, v uint32) {
w := walang.ModCallContextSys(m).Stdout()
b := v != 0
fmt.Fprint(w, b)
}).
WithParameterNames("v").
Export("waPrintBool").
// func waPrintI32(v: i32)
NewFunctionBuilder().
WithFunc(func(ctx context.Context, m api.Module, v int32) {
......
......@@ -38,6 +38,11 @@ func assert(ok: i32, pos_msg_ptr: i32, pos_msg_len: i32) {}
#wa:linkname $runtime.assertMessage
func assertMessage(ok: i32, msg_ptr: i32, msg_len: i32, pos_msg_ptr: i32, pos_msg_len: i32) {}
#wa:linkname $runtime.waPrintBool
func waPrintBool(i: bool) {
chrome.PrintBool(i)
}
#wa:linkname $runtime.waPrintI32
func waPrintI32(i: i32) {
chrome.PrintI32(i)
......
......@@ -58,6 +58,11 @@ func assertWithMessage(ok: i32, msg_ptr: i32, msg_len: i32, pos_msg_ptr: i32, po
}
}
#wa:linkname $runtime.waPrintBool
func waPrintBool(i: bool) {
mvp.PrintBool(i)
}
#wa:linkname $runtime.waPrintI32
func waPrintI32(i: i32) {
mvp.PrintI32(i)
......
......@@ -64,6 +64,22 @@ func puts(ptr: i32, len: i32)
#wa:linkname putchar
func putchar(ch: i32)
#wa:linkname $runtime.waPrintBool
func waPrintBool(i: bool) {
if i {
waPrintRune('t')
waPrintRune('r')
waPrintRune('u')
waPrintRune('e')
} else {
waPrintRune('f')
waPrintRune('a')
waPrintRune('l')
waPrintRune('s')
waPrintRune('e')
}
}
#wa:linkname $runtime.waPrintI32
func waPrintI32(i: i32) {
print_i32(i)
......
// 版权 @2023 凹语言 作者。保留所有权利。
#wa:import mvp waPrintBool
func PrintBool(i: bool)
#wa:import mvp waPrintI32
func PrintI32(i: i32)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册