Skip to content

Commit

Permalink
runtime: print/panic complex
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Jun 20, 2024
1 parent 4da59cd commit fc8ee77
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
8 changes: 4 additions & 4 deletions internal/runtime/z_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ func PrintFloat(v float64) {
}
return
}
c.Fprintf(c.Stderr, c.Str("%e"), v)
c.Fprintf(c.Stderr, c.Str("%+e"), v)
}

// func PrintComplex(c complex128) {
// print("(", real(c), imag(c), "i)")
// }
func PrintComplex(v complex128) {
c.Fprintf(c.Stderr, c.Str("(%e%+ei)"), real(v), imag(v))
}

func PrintUint(v uint64) {
c.Fprintf(c.Stderr, c.Str("%llu"), v)
Expand Down
4 changes: 4 additions & 0 deletions internal/runtime/z_rt.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func TracePanic(v any) {
} else {
println(*(*float64)(e.data))
}
case abi.Complex64:
println(*(*complex64)(e.data))
case abi.Complex128:
println(*(*complex128)(e.data))
case abi.String:
println(*(*string)(e.data))
default:
Expand Down
2 changes: 1 addition & 1 deletion ssa/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
27 changes: 25 additions & 2 deletions ssa/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 697 in ssa/expr.go

View check run for this annotation

Codecov / codecov/patch

ssa/expr.go#L688-L697

Added lines #L688 - L697 were not covered by tests
}
}
}
switch xtyp := x.RawType().Underlying().(type) {
case *types.Basic:
Expand Down Expand Up @@ -716,6 +728,16 @@ func (b Builder) Convert(t Type, x Expr) (ret Expr) {
}
}
}
if x.kind == vkComplex && t.kind == vkComplex {
ft := b.Prog.Complex128()
if t.raw.Type.Underlying().(*types.Basic).Kind() == types.Complex64 {
ft = b.Prog.Complex64()

Check warning on line 734 in ssa/expr.go

View check run for this annotation

Codecov / codecov/patch

ssa/expr.go#L731-L734

Added lines #L731 - L734 were not covered by tests
}
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

Check warning on line 739 in ssa/expr.go

View check run for this annotation

Codecov / codecov/patch

ssa/expr.go#L736-L739

Added lines #L736 - L739 were not covered by tests
}
case *types.Pointer:
ret.impl = castPtr(b.impl, x.impl, t.ll)
return
Expand Down Expand Up @@ -1013,8 +1035,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()

Check warning on line 1040 in ssa/expr.go

View check run for this annotation

Codecov / codecov/patch

ssa/expr.go#L1038-L1040

Added lines #L1038 - L1040 were not covered by tests
default:
panic(fmt.Errorf("illegal types for operand: print %v", arg.RawType()))
}
Expand Down
4 changes: 2 additions & 2 deletions ssa/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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())
}
Expand Down

0 comments on commit fc8ee77

Please sign in to comment.