github.com/goplus/igop@v0.25.0/unop.go (about)

     1  /*
     2   * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package igop
    18  
    19  import (
    20  	"reflect"
    21  
    22  	"github.com/visualfc/xtype"
    23  	"golang.org/x/tools/go/ssa"
    24  )
    25  
    26  func makeUnOpSUB(pfn *function, instr *ssa.UnOp) func(fr *frame) {
    27  	ir := pfn.regIndex(instr)
    28  	ix, kx, vx := pfn.regIndex3(instr.X)
    29  	typ := pfn.Interp.preToType(instr.Type())
    30  	if typ.PkgPath() == "" {
    31  		switch typ.Kind() {
    32  		case reflect.Int:
    33  			if kx == kindConst {
    34  				v := -vx.(int)
    35  				return func(fr *frame) { fr.setReg(ir, v) }
    36  			} else {
    37  				return func(fr *frame) {
    38  					v := -fr.reg(ix).(int)
    39  					fr.setReg(ir, v)
    40  				}
    41  			}
    42  		case reflect.Int8:
    43  			if kx == kindConst {
    44  				v := -vx.(int8)
    45  				return func(fr *frame) { fr.setReg(ir, v) }
    46  			} else {
    47  				return func(fr *frame) {
    48  					v := -fr.reg(ix).(int8)
    49  					fr.setReg(ir, v)
    50  				}
    51  			}
    52  		case reflect.Int16:
    53  			if kx == kindConst {
    54  				v := -vx.(int16)
    55  				return func(fr *frame) { fr.setReg(ir, v) }
    56  			} else {
    57  				return func(fr *frame) {
    58  					v := -fr.reg(ix).(int16)
    59  					fr.setReg(ir, v)
    60  				}
    61  			}
    62  		case reflect.Int32:
    63  			if kx == kindConst {
    64  				v := -vx.(int32)
    65  				return func(fr *frame) { fr.setReg(ir, v) }
    66  			} else {
    67  				return func(fr *frame) {
    68  					v := -fr.reg(ix).(int32)
    69  					fr.setReg(ir, v)
    70  				}
    71  			}
    72  		case reflect.Int64:
    73  			if kx == kindConst {
    74  				v := -vx.(int64)
    75  				return func(fr *frame) { fr.setReg(ir, v) }
    76  			} else {
    77  				return func(fr *frame) {
    78  					v := -fr.reg(ix).(int64)
    79  					fr.setReg(ir, v)
    80  				}
    81  			}
    82  		case reflect.Uint:
    83  			if kx == kindConst {
    84  				v := -vx.(uint)
    85  				return func(fr *frame) { fr.setReg(ir, v) }
    86  			} else {
    87  				return func(fr *frame) {
    88  					v := -fr.reg(ix).(uint)
    89  					fr.setReg(ir, v)
    90  				}
    91  			}
    92  		case reflect.Uint8:
    93  			if kx == kindConst {
    94  				v := -vx.(uint8)
    95  				return func(fr *frame) { fr.setReg(ir, v) }
    96  			} else {
    97  				return func(fr *frame) {
    98  					v := -fr.reg(ix).(uint8)
    99  					fr.setReg(ir, v)
   100  				}
   101  			}
   102  		case reflect.Uint16:
   103  			if kx == kindConst {
   104  				v := -vx.(uint16)
   105  				return func(fr *frame) { fr.setReg(ir, v) }
   106  			} else {
   107  				return func(fr *frame) {
   108  					v := -fr.reg(ix).(uint16)
   109  					fr.setReg(ir, v)
   110  				}
   111  			}
   112  		case reflect.Uint32:
   113  			if kx == kindConst {
   114  				v := -vx.(uint32)
   115  				return func(fr *frame) { fr.setReg(ir, v) }
   116  			} else {
   117  				return func(fr *frame) {
   118  					v := -fr.reg(ix).(uint32)
   119  					fr.setReg(ir, v)
   120  				}
   121  			}
   122  		case reflect.Uint64:
   123  			if kx == kindConst {
   124  				v := -vx.(uint64)
   125  				return func(fr *frame) { fr.setReg(ir, v) }
   126  			} else {
   127  				return func(fr *frame) {
   128  					v := -fr.reg(ix).(uint64)
   129  					fr.setReg(ir, v)
   130  				}
   131  			}
   132  		case reflect.Uintptr:
   133  			if kx == kindConst {
   134  				v := -vx.(uintptr)
   135  				return func(fr *frame) { fr.setReg(ir, v) }
   136  			} else {
   137  				return func(fr *frame) {
   138  					v := -fr.reg(ix).(uintptr)
   139  					fr.setReg(ir, v)
   140  				}
   141  			}
   142  		case reflect.Float32:
   143  			if kx == kindConst {
   144  				v := -vx.(float32)
   145  				return func(fr *frame) { fr.setReg(ir, v) }
   146  			} else {
   147  				return func(fr *frame) {
   148  					v := -fr.reg(ix).(float32)
   149  					fr.setReg(ir, v)
   150  				}
   151  			}
   152  		case reflect.Float64:
   153  			if kx == kindConst {
   154  				v := -vx.(float64)
   155  				return func(fr *frame) { fr.setReg(ir, v) }
   156  			} else {
   157  				return func(fr *frame) {
   158  					v := -fr.reg(ix).(float64)
   159  					fr.setReg(ir, v)
   160  				}
   161  			}
   162  		case reflect.Complex64:
   163  			if kx == kindConst {
   164  				v := -vx.(complex64)
   165  				return func(fr *frame) { fr.setReg(ir, v) }
   166  			} else {
   167  				return func(fr *frame) {
   168  					v := -fr.reg(ix).(complex64)
   169  					fr.setReg(ir, v)
   170  				}
   171  			}
   172  		case reflect.Complex128:
   173  			if kx == kindConst {
   174  				v := -vx.(complex128)
   175  				return func(fr *frame) { fr.setReg(ir, v) }
   176  			} else {
   177  				return func(fr *frame) {
   178  					v := -fr.reg(ix).(complex128)
   179  					fr.setReg(ir, v)
   180  				}
   181  			}
   182  		}
   183  	} else {
   184  		switch typ.Kind() {
   185  		case reflect.Int:
   186  			if kx == kindConst {
   187  				v := xtype.NegInt(vx)
   188  				return func(fr *frame) { fr.setReg(ir, v) }
   189  			} else {
   190  				return func(fr *frame) {
   191  					v := xtype.NegInt(fr.reg(ix))
   192  					fr.setReg(ir, v)
   193  				}
   194  			}
   195  		case reflect.Int8:
   196  			if kx == kindConst {
   197  				v := xtype.NegInt8(vx)
   198  				return func(fr *frame) { fr.setReg(ir, v) }
   199  			} else {
   200  				return func(fr *frame) {
   201  					v := xtype.NegInt8(fr.reg(ix))
   202  					fr.setReg(ir, v)
   203  				}
   204  			}
   205  		case reflect.Int16:
   206  			if kx == kindConst {
   207  				v := xtype.NegInt16(vx)
   208  				return func(fr *frame) { fr.setReg(ir, v) }
   209  			} else {
   210  				return func(fr *frame) {
   211  					v := xtype.NegInt16(fr.reg(ix))
   212  					fr.setReg(ir, v)
   213  				}
   214  			}
   215  		case reflect.Int32:
   216  			if kx == kindConst {
   217  				v := xtype.NegInt32(vx)
   218  				return func(fr *frame) { fr.setReg(ir, v) }
   219  			} else {
   220  				return func(fr *frame) {
   221  					v := xtype.NegInt32(fr.reg(ix))
   222  					fr.setReg(ir, v)
   223  				}
   224  			}
   225  		case reflect.Int64:
   226  			if kx == kindConst {
   227  				v := xtype.NegInt64(vx)
   228  				return func(fr *frame) { fr.setReg(ir, v) }
   229  			} else {
   230  				return func(fr *frame) {
   231  					v := xtype.NegInt64(fr.reg(ix))
   232  					fr.setReg(ir, v)
   233  				}
   234  			}
   235  		case reflect.Uint:
   236  			if kx == kindConst {
   237  				v := xtype.NegUint(vx)
   238  				return func(fr *frame) { fr.setReg(ir, v) }
   239  			} else {
   240  				return func(fr *frame) {
   241  					v := xtype.NegUint(fr.reg(ix))
   242  					fr.setReg(ir, v)
   243  				}
   244  			}
   245  		case reflect.Uint8:
   246  			if kx == kindConst {
   247  				v := xtype.NegUint8(vx)
   248  				return func(fr *frame) { fr.setReg(ir, v) }
   249  			} else {
   250  				return func(fr *frame) {
   251  					v := xtype.NegUint8(fr.reg(ix))
   252  					fr.setReg(ir, v)
   253  				}
   254  			}
   255  		case reflect.Uint16:
   256  			if kx == kindConst {
   257  				v := xtype.NegUint16(vx)
   258  				return func(fr *frame) { fr.setReg(ir, v) }
   259  			} else {
   260  				return func(fr *frame) {
   261  					v := xtype.NegUint16(fr.reg(ix))
   262  					fr.setReg(ir, v)
   263  				}
   264  			}
   265  		case reflect.Uint32:
   266  			if kx == kindConst {
   267  				v := xtype.NegUint32(vx)
   268  				return func(fr *frame) { fr.setReg(ir, v) }
   269  			} else {
   270  				return func(fr *frame) {
   271  					v := xtype.NegUint32(fr.reg(ix))
   272  					fr.setReg(ir, v)
   273  				}
   274  			}
   275  		case reflect.Uint64:
   276  			if kx == kindConst {
   277  				v := xtype.NegUint64(vx)
   278  				return func(fr *frame) { fr.setReg(ir, v) }
   279  			} else {
   280  				return func(fr *frame) {
   281  					v := xtype.NegUint64(fr.reg(ix))
   282  					fr.setReg(ir, v)
   283  				}
   284  			}
   285  		case reflect.Uintptr:
   286  			if kx == kindConst {
   287  				v := xtype.NegUintptr(vx)
   288  				return func(fr *frame) { fr.setReg(ir, v) }
   289  			} else {
   290  				return func(fr *frame) {
   291  					v := xtype.NegUintptr(fr.reg(ix))
   292  					fr.setReg(ir, v)
   293  				}
   294  			}
   295  		case reflect.Float32:
   296  			if kx == kindConst {
   297  				v := xtype.NegFloat32(vx)
   298  				return func(fr *frame) { fr.setReg(ir, v) }
   299  			} else {
   300  				return func(fr *frame) {
   301  					v := xtype.NegFloat32(fr.reg(ix))
   302  					fr.setReg(ir, v)
   303  				}
   304  			}
   305  		case reflect.Float64:
   306  			if kx == kindConst {
   307  				v := xtype.NegFloat64(vx)
   308  				return func(fr *frame) { fr.setReg(ir, v) }
   309  			} else {
   310  				return func(fr *frame) {
   311  					v := xtype.NegFloat64(fr.reg(ix))
   312  					fr.setReg(ir, v)
   313  				}
   314  			}
   315  		case reflect.Complex64:
   316  			if kx == kindConst {
   317  				v := xtype.NegComplex64(vx)
   318  				return func(fr *frame) { fr.setReg(ir, v) }
   319  			} else {
   320  				return func(fr *frame) {
   321  					v := xtype.NegComplex64(fr.reg(ix))
   322  					fr.setReg(ir, v)
   323  				}
   324  			}
   325  		case reflect.Complex128:
   326  			if kx == kindConst {
   327  				v := xtype.NegComplex128(vx)
   328  				return func(fr *frame) { fr.setReg(ir, v) }
   329  			} else {
   330  				return func(fr *frame) {
   331  					v := xtype.NegComplex128(fr.reg(ix))
   332  					fr.setReg(ir, v)
   333  				}
   334  			}
   335  		}
   336  	}
   337  	panic("unreachable")
   338  }
   339  func makeUnOpXOR(pfn *function, instr *ssa.UnOp) func(fr *frame) {
   340  	ir := pfn.regIndex(instr)
   341  	ix, kx, vx := pfn.regIndex3(instr.X)
   342  	typ := pfn.Interp.preToType(instr.Type())
   343  	if typ.PkgPath() == "" {
   344  		switch typ.Kind() {
   345  		case reflect.Int:
   346  			if kx == kindConst {
   347  				v := ^vx.(int)
   348  				return func(fr *frame) { fr.setReg(ir, v) }
   349  			} else {
   350  				return func(fr *frame) {
   351  					v := ^fr.reg(ix).(int)
   352  					fr.setReg(ir, v)
   353  				}
   354  			}
   355  		case reflect.Int8:
   356  			if kx == kindConst {
   357  				v := ^vx.(int8)
   358  				return func(fr *frame) { fr.setReg(ir, v) }
   359  			} else {
   360  				return func(fr *frame) {
   361  					v := ^fr.reg(ix).(int8)
   362  					fr.setReg(ir, v)
   363  				}
   364  			}
   365  		case reflect.Int16:
   366  			if kx == kindConst {
   367  				v := ^vx.(int16)
   368  				return func(fr *frame) { fr.setReg(ir, v) }
   369  			} else {
   370  				return func(fr *frame) {
   371  					v := ^fr.reg(ix).(int16)
   372  					fr.setReg(ir, v)
   373  				}
   374  			}
   375  		case reflect.Int32:
   376  			if kx == kindConst {
   377  				v := ^vx.(int32)
   378  				return func(fr *frame) { fr.setReg(ir, v) }
   379  			} else {
   380  				return func(fr *frame) {
   381  					v := ^fr.reg(ix).(int32)
   382  					fr.setReg(ir, v)
   383  				}
   384  			}
   385  		case reflect.Int64:
   386  			if kx == kindConst {
   387  				v := ^vx.(int64)
   388  				return func(fr *frame) { fr.setReg(ir, v) }
   389  			} else {
   390  				return func(fr *frame) {
   391  					v := ^fr.reg(ix).(int64)
   392  					fr.setReg(ir, v)
   393  				}
   394  			}
   395  		case reflect.Uint:
   396  			if kx == kindConst {
   397  				v := ^vx.(uint)
   398  				return func(fr *frame) { fr.setReg(ir, v) }
   399  			} else {
   400  				return func(fr *frame) {
   401  					v := ^fr.reg(ix).(uint)
   402  					fr.setReg(ir, v)
   403  				}
   404  			}
   405  		case reflect.Uint8:
   406  			if kx == kindConst {
   407  				v := ^vx.(uint8)
   408  				return func(fr *frame) { fr.setReg(ir, v) }
   409  			} else {
   410  				return func(fr *frame) {
   411  					v := ^fr.reg(ix).(uint8)
   412  					fr.setReg(ir, v)
   413  				}
   414  			}
   415  		case reflect.Uint16:
   416  			if kx == kindConst {
   417  				v := ^vx.(uint16)
   418  				return func(fr *frame) { fr.setReg(ir, v) }
   419  			} else {
   420  				return func(fr *frame) {
   421  					v := ^fr.reg(ix).(uint16)
   422  					fr.setReg(ir, v)
   423  				}
   424  			}
   425  		case reflect.Uint32:
   426  			if kx == kindConst {
   427  				v := ^vx.(uint32)
   428  				return func(fr *frame) { fr.setReg(ir, v) }
   429  			} else {
   430  				return func(fr *frame) {
   431  					v := ^fr.reg(ix).(uint32)
   432  					fr.setReg(ir, v)
   433  				}
   434  			}
   435  		case reflect.Uint64:
   436  			if kx == kindConst {
   437  				v := ^vx.(uint64)
   438  				return func(fr *frame) { fr.setReg(ir, v) }
   439  			} else {
   440  				return func(fr *frame) {
   441  					v := ^fr.reg(ix).(uint64)
   442  					fr.setReg(ir, v)
   443  				}
   444  			}
   445  		case reflect.Uintptr:
   446  			if kx == kindConst {
   447  				v := ^vx.(uintptr)
   448  				return func(fr *frame) { fr.setReg(ir, v) }
   449  			} else {
   450  				return func(fr *frame) {
   451  					v := ^fr.reg(ix).(uintptr)
   452  					fr.setReg(ir, v)
   453  				}
   454  			}
   455  		}
   456  	} else {
   457  		switch typ.Kind() {
   458  		case reflect.Int:
   459  			if kx == kindConst {
   460  				v := xtype.XorInt(vx)
   461  				return func(fr *frame) { fr.setReg(ir, v) }
   462  			} else {
   463  				return func(fr *frame) {
   464  					v := xtype.XorInt(fr.reg(ix))
   465  					fr.setReg(ir, v)
   466  				}
   467  			}
   468  		case reflect.Int8:
   469  			if kx == kindConst {
   470  				v := xtype.XorInt8(vx)
   471  				return func(fr *frame) { fr.setReg(ir, v) }
   472  			} else {
   473  				return func(fr *frame) {
   474  					v := xtype.XorInt8(fr.reg(ix))
   475  					fr.setReg(ir, v)
   476  				}
   477  			}
   478  		case reflect.Int16:
   479  			if kx == kindConst {
   480  				v := xtype.XorInt16(vx)
   481  				return func(fr *frame) { fr.setReg(ir, v) }
   482  			} else {
   483  				return func(fr *frame) {
   484  					v := xtype.XorInt16(fr.reg(ix))
   485  					fr.setReg(ir, v)
   486  				}
   487  			}
   488  		case reflect.Int32:
   489  			if kx == kindConst {
   490  				v := xtype.XorInt32(vx)
   491  				return func(fr *frame) { fr.setReg(ir, v) }
   492  			} else {
   493  				return func(fr *frame) {
   494  					v := xtype.XorInt32(fr.reg(ix))
   495  					fr.setReg(ir, v)
   496  				}
   497  			}
   498  		case reflect.Int64:
   499  			if kx == kindConst {
   500  				v := xtype.XorInt64(vx)
   501  				return func(fr *frame) { fr.setReg(ir, v) }
   502  			} else {
   503  				return func(fr *frame) {
   504  					v := xtype.XorInt64(fr.reg(ix))
   505  					fr.setReg(ir, v)
   506  				}
   507  			}
   508  		case reflect.Uint:
   509  			if kx == kindConst {
   510  				v := xtype.XorUint(vx)
   511  				return func(fr *frame) { fr.setReg(ir, v) }
   512  			} else {
   513  				return func(fr *frame) {
   514  					v := xtype.XorUint(fr.reg(ix))
   515  					fr.setReg(ir, v)
   516  				}
   517  			}
   518  		case reflect.Uint8:
   519  			if kx == kindConst {
   520  				v := xtype.XorUint8(vx)
   521  				return func(fr *frame) { fr.setReg(ir, v) }
   522  			} else {
   523  				return func(fr *frame) {
   524  					v := xtype.XorUint8(fr.reg(ix))
   525  					fr.setReg(ir, v)
   526  				}
   527  			}
   528  		case reflect.Uint16:
   529  			if kx == kindConst {
   530  				v := xtype.XorUint16(vx)
   531  				return func(fr *frame) { fr.setReg(ir, v) }
   532  			} else {
   533  				return func(fr *frame) {
   534  					v := xtype.XorUint16(fr.reg(ix))
   535  					fr.setReg(ir, v)
   536  				}
   537  			}
   538  		case reflect.Uint32:
   539  			if kx == kindConst {
   540  				v := xtype.XorUint32(vx)
   541  				return func(fr *frame) { fr.setReg(ir, v) }
   542  			} else {
   543  				return func(fr *frame) {
   544  					v := xtype.XorUint32(fr.reg(ix))
   545  					fr.setReg(ir, v)
   546  				}
   547  			}
   548  		case reflect.Uint64:
   549  			if kx == kindConst {
   550  				v := xtype.XorUint64(vx)
   551  				return func(fr *frame) { fr.setReg(ir, v) }
   552  			} else {
   553  				return func(fr *frame) {
   554  					v := xtype.XorUint64(fr.reg(ix))
   555  					fr.setReg(ir, v)
   556  				}
   557  			}
   558  		case reflect.Uintptr:
   559  			if kx == kindConst {
   560  				v := xtype.XorUintptr(vx)
   561  				return func(fr *frame) { fr.setReg(ir, v) }
   562  			} else {
   563  				return func(fr *frame) {
   564  					v := xtype.XorUintptr(fr.reg(ix))
   565  					fr.setReg(ir, v)
   566  				}
   567  			}
   568  		}
   569  	}
   570  	panic("unreachable")
   571  }