From acc5de8d14f2c655149ed241ddf0eb2941084dcf Mon Sep 17 00:00:00 2001 From: visualfc Date: Mon, 29 Apr 2024 22:19:08 +0800 Subject: [PATCH] ssa: builder.const add float --- ssa/expr.go | 11 ++++++++++- ssa/type.go | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ssa/expr.go b/ssa/expr.go index 822e7172..f3a07702 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -107,6 +107,11 @@ func (p Program) IntVal(v uint64, t Type) Expr { return Expr{ret, t} } +func (p Program) FloatVal(v float64, t Type) Expr { + ret := llvm.ConstFloat(t.ll, v) + return Expr{ret, t} +} + // Val returns a constant expression. func (p Program) Val(v interface{}) Expr { switch v := v.(type) { @@ -130,7 +135,7 @@ func (b Builder) Const(v constant.Value, typ Type) Expr { if v == nil { return prog.Null(typ) } - switch t := typ.t.(type) { + switch t := types.Default(typ.t).(type) { case *types.Basic: kind := t.Kind() switch { @@ -140,6 +145,10 @@ func (b Builder) Const(v constant.Value, typ Type) Expr { if v, exact := constant.Uint64Val(v); exact { return prog.IntVal(v, typ) } + case kind == types.Float32 || kind == types.Float64: + if v, exact := constant.Float64Val(v); exact { + return prog.FloatVal(v, typ) + } case kind == types.String: return prog.StringVal(constant.StringVal(v)) } diff --git a/ssa/type.go b/ssa/type.go index 6b02513a..b5e4bb85 100644 --- a/ssa/type.go +++ b/ssa/type.go @@ -208,7 +208,7 @@ func (p Program) tyInt64() llvm.Type { } func (p Program) toLLVMType(typ types.Type) Type { - switch t := typ.(type) { + switch t := types.Default(typ).(type) { case *types.Basic: switch t.Kind() { case types.Int: