github.com/goplus/gossa@v0.3.25/opcvt_gen.go (about) 1 //go:build ignore 2 // +build ignore 3 4 package main 5 6 import ( 7 "bytes" 8 "fmt" 9 "go/format" 10 "io/ioutil" 11 "os" 12 "strings" 13 ) 14 15 var ( 16 ints = []string{"int", "int8", "int16", "int32", "int64", 17 "uint", "uint8", "uint16", "uint32", "uint64", "uintptr"} 18 floats = []string{"float32", "float64"} 19 comps = []string{"complex64", "complex128"} 20 ) 21 22 func json(kinds ...[]string) (r []string) { 23 for _, kind := range kinds { 24 r = append(r, kind...) 25 } 26 return 27 } 28 29 func main() { 30 var buf bytes.Buffer 31 buf.WriteString(pkg_head) 32 33 for _, typ := range ints { 34 makeFuncOp(&buf, "cvt"+strings.Title(typ), typ) 35 } 36 for _, typ := range floats { 37 makeFuncOp(&buf, "cvt"+strings.Title(typ), typ) 38 } 39 for _, typ := range comps { 40 makeFuncOp2(&buf, "cvt"+strings.Title(typ), typ) 41 } 42 43 data, err := format.Source(buf.Bytes()) 44 if err != nil { 45 fmt.Println("format error", err) 46 os.Exit(2) 47 } 48 ioutil.WriteFile("./opcvt.go", data, 0666) 49 } 50 51 func makeFuncOp(buf *bytes.Buffer, fnname string, typ string) { 52 r := strings.NewReplacer("cvtInt", fnname, "type T = int", "type T = "+typ) 53 r.WriteString(buf, cvt_func) 54 } 55 56 func makeFuncOp2(buf *bytes.Buffer, fnname string, typ string) { 57 r := strings.NewReplacer("cvtComplex64", fnname, "type T = complex64", "type T = "+typ) 58 r.WriteString(buf, cvt_func2) 59 } 60 61 var pkg_head = `package gossa 62 63 import ( 64 "reflect" 65 66 "github.com/goplus/gossa/internal/xtype" 67 ) 68 ` 69 70 var cvt_func = `func cvtInt(ir, ix register, xkind reflect.Kind, xtyp reflect.Type, typ reflect.Type) func(fr *frame) { 71 type T = int 72 t := xtype.TypeOfType(typ) 73 isBasic := typ.PkgPath() == "" 74 if xtyp.PkgPath() == "" { 75 return func(fr *frame) { 76 var v T 77 switch xkind { 78 case reflect.Int: 79 v = T(fr.reg(ix).(int)) 80 case reflect.Int8: 81 v = T(fr.reg(ix).(int8)) 82 case reflect.Int16: 83 v = T(fr.reg(ix).(int16)) 84 case reflect.Int32: 85 v = T(fr.reg(ix).(int32)) 86 case reflect.Int64: 87 v = T(fr.reg(ix).(int64)) 88 case reflect.Uint: 89 v = T(fr.reg(ix).(uint)) 90 case reflect.Uint8: 91 v = T(fr.reg(ix).(uint8)) 92 case reflect.Uint16: 93 v = T(fr.reg(ix).(uint16)) 94 case reflect.Uint32: 95 v = T(fr.reg(ix).(uint32)) 96 case reflect.Uint64: 97 v = T(fr.reg(ix).(uint64)) 98 case reflect.Uintptr: 99 v = T(fr.reg(ix).(uintptr)) 100 case reflect.Float32: 101 v = T(fr.reg(ix).(float32)) 102 case reflect.Float64: 103 v = T(fr.reg(ix).(float64)) 104 } 105 if isBasic { 106 fr.setReg(ir, v) 107 } else { 108 fr.setReg(ir, xtype.Make(t, v)) 109 } 110 } 111 } else { 112 return func(fr *frame) { 113 var v T 114 switch xkind { 115 case reflect.Int: 116 v = T(fr.int(ix)) 117 case reflect.Int8: 118 v = T(fr.int8(ix)) 119 case reflect.Int16: 120 v = T(fr.int16(ix)) 121 case reflect.Int32: 122 v = T(fr.int32(ix)) 123 case reflect.Int64: 124 v = T(fr.int64(ix)) 125 case reflect.Uint: 126 v = T(fr.uint(ix)) 127 case reflect.Uint8: 128 v = T(fr.uint8(ix)) 129 case reflect.Uint16: 130 v = T(fr.uint16(ix)) 131 case reflect.Uint32: 132 v = T(fr.uint32(ix)) 133 case reflect.Uint64: 134 v = T(fr.uint64(ix)) 135 case reflect.Uintptr: 136 v = T(fr.uintptr(ix)) 137 case reflect.Float32: 138 v = T(fr.float32(ix)) 139 case reflect.Float64: 140 v = T(fr.float64(ix)) 141 } 142 if isBasic { 143 fr.setReg(ir, v) 144 } else { 145 fr.setReg(ir, xtype.Make(t, v)) 146 } 147 } 148 } 149 } 150 ` 151 152 var cvt_func2 = `func cvtComplex64(ir, ix register, xkind reflect.Kind, xtyp reflect.Type, typ reflect.Type) func(fr *frame) { 153 type T = complex64 154 t := xtype.TypeOfType(typ) 155 isBasic := typ.PkgPath() == "" 156 if xtyp.PkgPath() == "" { 157 return func(fr *frame) { 158 var v T 159 switch xkind { 160 case reflect.Complex64: 161 v = T(fr.reg(ix).(complex64)) 162 case reflect.Complex128: 163 v = T(fr.reg(ix).(complex128)) 164 } 165 if isBasic { 166 fr.setReg(ir, v) 167 } else { 168 fr.setReg(ir, xtype.Make(t, v)) 169 } 170 } 171 } else { 172 return func(fr *frame) { 173 var v T 174 switch xkind { 175 case reflect.Complex64: 176 v = T(fr.complex64(ix)) 177 case reflect.Complex128: 178 v = T(fr.complex128(ix)) 179 } 180 if isBasic { 181 fr.setReg(ir, v) 182 } else { 183 fr.setReg(ir, xtype.Make(t, v)) 184 } 185 } 186 } 187 } 188 `