runtime: print/panic complex

This commit is contained in:
visualfc
2024-06-20 15:52:56 +08:00
parent 32883b4e18
commit 02a5375503
7 changed files with 114 additions and 9 deletions

View File

@@ -89,7 +89,7 @@ func DataKindOf(raw types.Type, lvl int, is32Bits bool) (DataKind, types.Type, i
return Integer, raw, lvl
case kind == types.Float32:
return BitCast, raw, lvl
case kind == types.Float64 || kind == types.Complex64:
case kind == types.Float64:
if is32Bits {
return Indirect, raw, lvl
}

View File

@@ -685,6 +685,18 @@ func (b Builder) Convert(t Type, x Expr) (ret Expr) {
ret.impl = b.InlineCall(b.Func.Pkg.rtFunc("StringFromRune"), x).impl
return
}
case types.Complex128:
switch xtyp := x.RawType().Underlying().(type) {
case *types.Basic:
if xtyp.Kind() == types.Complex64 {
r := b.impl.CreateExtractValue(x.impl, 0, "")
i := b.impl.CreateExtractValue(x.impl, 1, "")
r = castFloat(b, r, b.Prog.Float64())
i = castFloat(b, i, b.Prog.Float64())
ret.impl = b.aggregateValue(t, r, i).impl
return
}
}
}
switch xtyp := x.RawType().Underlying().(type) {
case *types.Basic:
@@ -716,6 +728,16 @@ func (b Builder) Convert(t Type, x Expr) (ret Expr) {
}
}
}
if x.kind == vkComplex && t.kind == vkComplex {
ft := b.Prog.Float64()
if t.raw.Type.Underlying().(*types.Basic).Kind() == types.Complex64 {
ft = b.Prog.Float32()
}
r := b.impl.CreateExtractValue(x.impl, 0, "")
i := b.impl.CreateExtractValue(x.impl, 1, "")
ret.impl = b.Complex(Expr{castFloat(b, r, ft), ft}, Expr{castFloat(b, i, ft), ft}).impl
return
}
case *types.Pointer:
ret.impl = castPtr(b.impl, x.impl, t.ll)
return
@@ -1015,8 +1037,9 @@ func (b Builder) PrintEx(ln bool, args ...Expr) (ret Expr) {
fn = "PrintEface"
case vkIface:
fn = "PrintIface"
// case vkComplex:
// fn = "PrintComplex"
case vkComplex:
fn = "PrintComplex"
typ = prog.Complex128()
default:
panic(fmt.Errorf("illegal types for operand: print %v", arg.RawType()))
}

View File

@@ -211,7 +211,7 @@ func (p Program) Index(typ Type) Type {
func (p Program) Field(typ Type, i int) Type {
var fld *types.Var
switch t := typ.raw.Type.(type) {
switch t := typ.raw.Type.Underlying().(type) {
case *types.Tuple:
fld = t.At(i)
case *types.Basic:
@@ -223,7 +223,7 @@ func (p Program) Field(typ Type, i int) Type {
}
panic("Field: basic type doesn't have fields")
default:
fld = t.Underlying().(*types.Struct).Field(i)
fld = t.(*types.Struct).Field(i)
}
return p.rawType(fld.Type())
}