github.com/cloudwego/frugal@v0.1.15/internal/atm/ssa/utils.go (about) 1 /* 2 * Copyright 2022 ByteDance Inc. 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 ssa 18 19 import ( 20 `math` 21 `strings` 22 `unsafe` 23 24 `github.com/cloudwego/frugal/internal/atm/hir` 25 `github.com/oleiade/lane` 26 ) 27 28 func isu8(v int64) bool { 29 return v >= 0 && v <= math.MaxUint8 30 } 31 32 func isi32(v int64) bool { 33 return v >= math.MinInt32 && v <= math.MaxInt32 34 } 35 36 func isp32(p unsafe.Pointer) bool { 37 return uintptr(p) <= math.MaxInt32 38 } 39 40 func ri2reg(ri uint8) Reg { 41 if ri & hir.ArgPointer == 0 { 42 return Rv(hir.GenericRegister(ri & hir.ArgMask)) 43 } else { 44 return Rv(hir.PointerRegister(ri & hir.ArgMask)) 45 } 46 } 47 48 func ri2regz(ri []uint8) Reg { 49 switch len(ri) { 50 case 0 : return Rz 51 case 1 : return ri2reg(ri[0]) 52 default : panic("invalid register count") 53 } 54 } 55 56 func ri2regs(ri []uint8) []Reg { 57 ret := make([]Reg, len(ri)) 58 for i, r := range ri { ret[i] = ri2reg(r) } 59 return ret 60 } 61 62 func minint(a int, b int) int { 63 if a < b { 64 return a 65 } else { 66 return b 67 } 68 } 69 70 func cmpu64(a uint64, b uint64) int { 71 if a < b { 72 return -1 73 } else if a > b { 74 return 1 75 } else { 76 return 0 77 } 78 } 79 80 func addptr(p unsafe.Pointer, i int64) unsafe.Pointer { 81 return unsafe.Pointer(uintptr(p) + uintptr(i)) 82 } 83 84 func int2bool(v int) bool { 85 return v != 0 86 } 87 88 func bool2int(v bool) int { 89 if v { 90 return 1 91 } else { 92 return 0 93 } 94 } 95 96 func memsizec(n uint8) rune { 97 switch n { 98 case 1 : return 'b' 99 case 2 : return 'w' 100 case 4 : return 'l' 101 case 8 : return 'q' 102 default : panic("unreachable") 103 } 104 } 105 106 func stacknew(v interface{}) (r *lane.Stack) { 107 r = lane.NewStack() 108 r.Push(v) 109 return 110 } 111 112 func regorder(v Reg) int { 113 if v.Kind() != K_arch { 114 return int(v) 115 } else { 116 return math.MinInt64 117 } 118 } 119 120 func regnewref(v Reg) (r *Reg) { 121 r = new(Reg) 122 *r = v 123 return 124 } 125 126 func regsliceref(v []Reg) (r []*Reg) { 127 r = make([]*Reg, len(v)) 128 for i := range v { r[i] = &v[i] } 129 return 130 } 131 132 func regslicerepr(v []Reg) string { 133 r := make([]string, 0, len(v)) 134 for _, x := range v { r = append(r, x.String()) } 135 return strings.Join(r, ", ") 136 } 137 138 func regsliceclone(v []Reg) (r []Reg) { 139 r = make([]Reg, len(v)) 140 copy(r, v) 141 return 142 } 143 144 func blockreverse(s []*BasicBlock) { 145 for i, j := 0, len(s) - 1; i < j; i, j = i + 1, j - 1 { 146 s[i], s[j] = s[j], s[i] 147 } 148 }