Merge pull request #77 from visualfc/float

ssa: builder.const add float
This commit is contained in:
xushiwei
2024-04-29 23:09:22 +08:00
committed by GitHub
2 changed files with 11 additions and 2 deletions

View File

@@ -107,6 +107,11 @@ func (p Program) IntVal(v uint64, t Type) Expr {
return Expr{ret, t} 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. // Val returns a constant expression.
func (p Program) Val(v interface{}) Expr { func (p Program) Val(v interface{}) Expr {
switch v := v.(type) { switch v := v.(type) {
@@ -130,7 +135,7 @@ func (b Builder) Const(v constant.Value, typ Type) Expr {
if v == nil { if v == nil {
return prog.Null(typ) return prog.Null(typ)
} }
switch t := typ.t.(type) { switch t := types.Default(typ.t).(type) {
case *types.Basic: case *types.Basic:
kind := t.Kind() kind := t.Kind()
switch { switch {
@@ -140,6 +145,10 @@ func (b Builder) Const(v constant.Value, typ Type) Expr {
if v, exact := constant.Uint64Val(v); exact { if v, exact := constant.Uint64Val(v); exact {
return prog.IntVal(v, typ) 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: case kind == types.String:
return prog.StringVal(constant.StringVal(v)) return prog.StringVal(constant.StringVal(v))
} }

View File

@@ -208,7 +208,7 @@ func (p Program) tyInt64() llvm.Type {
} }
func (p Program) toLLVMType(typ types.Type) Type { func (p Program) toLLVMType(typ types.Type) Type {
switch t := typ.(type) { switch t := types.Default(typ).(type) {
case *types.Basic: case *types.Basic:
switch t.Kind() { switch t.Kind() {
case types.Int: case types.Int: