github.com/goplus/igop@v0.25.0/opcvt_gen.go (about) 1 //go:build ignore 2 // +build ignore 3 4 /* 5 * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 package main 21 22 import ( 23 "bytes" 24 "fmt" 25 "go/format" 26 "io/ioutil" 27 "os" 28 "strings" 29 ) 30 31 var ( 32 ints = []string{"int", "int8", "int16", "int32", "int64", 33 "uint", "uint8", "uint16", "uint32", "uint64", "uintptr"} 34 floats = []string{"float32", "float64"} 35 comps = []string{"complex64", "complex128"} 36 ) 37 38 func json(kinds ...[]string) (r []string) { 39 for _, kind := range kinds { 40 r = append(r, kind...) 41 } 42 return 43 } 44 45 func main() { 46 var buf bytes.Buffer 47 buf.WriteString(pkg_head) 48 49 for _, typ := range ints { 50 makeFuncOp(&buf, "cvt"+strings.Title(typ), typ) 51 } 52 for _, typ := range floats { 53 makeFuncOp(&buf, "cvt"+strings.Title(typ), typ) 54 } 55 for _, typ := range comps { 56 makeFuncOp2(&buf, "cvt"+strings.Title(typ), typ) 57 } 58 59 data, err := format.Source(buf.Bytes()) 60 if err != nil { 61 fmt.Println("format error", err) 62 os.Exit(2) 63 } 64 ioutil.WriteFile("./opcvt.go", data, 0666) 65 } 66 67 func makeFuncOp(buf *bytes.Buffer, fnname string, typ string) { 68 r := strings.NewReplacer("cvtInt", fnname, "type T = int", "type T = "+typ) 69 r.WriteString(buf, cvt_func) 70 } 71 72 func makeFuncOp2(buf *bytes.Buffer, fnname string, typ string) { 73 r := strings.NewReplacer("cvtComplex64", fnname, "type T = complex64", "type T = "+typ) 74 r.WriteString(buf, cvt_func2) 75 } 76 77 var pkg_head = `/* 78 * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved. 79 * 80 * Licensed under the Apache License, Version 2.0 (the "License"); 81 * you may not use this file except in compliance with the License. 82 * You may obtain a copy of the License at 83 * 84 * http://www.apache.org/licenses/LICENSE-2.0 85 * 86 * Unless required by applicable law or agreed to in writing, software 87 * distributed under the License is distributed on an "AS IS" BASIS, 88 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 89 * See the License for the specific language governing permissions and 90 * limitations under the License. 91 */ 92 93 package igop 94 95 import ( 96 "reflect" 97 98 "github.com/visualfc/xtype" 99 ) 100 ` 101 102 var cvt_func = `func cvtInt(ir, ix register, xkind reflect.Kind, xtyp reflect.Type, typ reflect.Type) func(fr *frame) { 103 type T = int 104 t := xtype.TypeOfType(typ) 105 isBasic := typ.PkgPath() == "" 106 if xtyp.PkgPath() == "" { 107 return func(fr *frame) { 108 var v T 109 switch xkind { 110 case reflect.Int: 111 v = T(fr.reg(ix).(int)) 112 case reflect.Int8: 113 v = T(fr.reg(ix).(int8)) 114 case reflect.Int16: 115 v = T(fr.reg(ix).(int16)) 116 case reflect.Int32: 117 v = T(fr.reg(ix).(int32)) 118 case reflect.Int64: 119 v = T(fr.reg(ix).(int64)) 120 case reflect.Uint: 121 v = T(fr.reg(ix).(uint)) 122 case reflect.Uint8: 123 v = T(fr.reg(ix).(uint8)) 124 case reflect.Uint16: 125 v = T(fr.reg(ix).(uint16)) 126 case reflect.Uint32: 127 v = T(fr.reg(ix).(uint32)) 128 case reflect.Uint64: 129 v = T(fr.reg(ix).(uint64)) 130 case reflect.Uintptr: 131 v = T(fr.reg(ix).(uintptr)) 132 case reflect.Float32: 133 v = T(fr.reg(ix).(float32)) 134 case reflect.Float64: 135 v = T(fr.reg(ix).(float64)) 136 } 137 if isBasic { 138 fr.setReg(ir, v) 139 } else { 140 fr.setReg(ir, xtype.Make(t, v)) 141 } 142 } 143 } else { 144 return func(fr *frame) { 145 var v T 146 switch xkind { 147 case reflect.Int: 148 v = T(fr.int(ix)) 149 case reflect.Int8: 150 v = T(fr.int8(ix)) 151 case reflect.Int16: 152 v = T(fr.int16(ix)) 153 case reflect.Int32: 154 v = T(fr.int32(ix)) 155 case reflect.Int64: 156 v = T(fr.int64(ix)) 157 case reflect.Uint: 158 v = T(fr.uint(ix)) 159 case reflect.Uint8: 160 v = T(fr.uint8(ix)) 161 case reflect.Uint16: 162 v = T(fr.uint16(ix)) 163 case reflect.Uint32: 164 v = T(fr.uint32(ix)) 165 case reflect.Uint64: 166 v = T(fr.uint64(ix)) 167 case reflect.Uintptr: 168 v = T(fr.uintptr(ix)) 169 case reflect.Float32: 170 v = T(fr.float32(ix)) 171 case reflect.Float64: 172 v = T(fr.float64(ix)) 173 } 174 if isBasic { 175 fr.setReg(ir, v) 176 } else { 177 fr.setReg(ir, xtype.Make(t, v)) 178 } 179 } 180 } 181 } 182 ` 183 184 var cvt_func2 = `func cvtComplex64(ir, ix register, xkind reflect.Kind, xtyp reflect.Type, typ reflect.Type) func(fr *frame) { 185 type T = complex64 186 t := xtype.TypeOfType(typ) 187 isBasic := typ.PkgPath() == "" 188 if xtyp.PkgPath() == "" { 189 return func(fr *frame) { 190 var v T 191 switch xkind { 192 case reflect.Complex64: 193 v = T(fr.reg(ix).(complex64)) 194 case reflect.Complex128: 195 v = T(fr.reg(ix).(complex128)) 196 } 197 if isBasic { 198 fr.setReg(ir, v) 199 } else { 200 fr.setReg(ir, xtype.Make(t, v)) 201 } 202 } 203 } else { 204 return func(fr *frame) { 205 var v T 206 switch xkind { 207 case reflect.Complex64: 208 v = T(fr.complex64(ix)) 209 case reflect.Complex128: 210 v = T(fr.complex128(ix)) 211 } 212 if isBasic { 213 fr.setReg(ir, v) 214 } else { 215 fr.setReg(ir, xtype.Make(t, v)) 216 } 217 } 218 } 219 } 220 `