github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/inline/inlheur/serialize.go (about) 1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package inlheur 6 7 import "strings" 8 9 func (funcProps *FuncProps) SerializeToString() string { 10 if funcProps == nil { 11 return "" 12 } 13 var sb strings.Builder 14 writeUleb128(&sb, uint64(funcProps.Flags)) 15 writeUleb128(&sb, uint64(len(funcProps.ParamFlags))) 16 for _, pf := range funcProps.ParamFlags { 17 writeUleb128(&sb, uint64(pf)) 18 } 19 writeUleb128(&sb, uint64(len(funcProps.ResultFlags))) 20 for _, rf := range funcProps.ResultFlags { 21 writeUleb128(&sb, uint64(rf)) 22 } 23 return sb.String() 24 } 25 26 func DeserializeFromString(s string) *FuncProps { 27 if len(s) == 0 { 28 return nil 29 } 30 var funcProps FuncProps 31 var v uint64 32 sl := []byte(s) 33 v, sl = readULEB128(sl) 34 funcProps.Flags = FuncPropBits(v) 35 v, sl = readULEB128(sl) 36 funcProps.ParamFlags = make([]ParamPropBits, v) 37 for i := range funcProps.ParamFlags { 38 v, sl = readULEB128(sl) 39 funcProps.ParamFlags[i] = ParamPropBits(v) 40 } 41 v, sl = readULEB128(sl) 42 funcProps.ResultFlags = make([]ResultPropBits, v) 43 for i := range funcProps.ResultFlags { 44 v, sl = readULEB128(sl) 45 funcProps.ResultFlags[i] = ResultPropBits(v) 46 } 47 return &funcProps 48 } 49 50 func readULEB128(sl []byte) (value uint64, rsl []byte) { 51 var shift uint 52 53 for { 54 b := sl[0] 55 sl = sl[1:] 56 value |= (uint64(b&0x7F) << shift) 57 if b&0x80 == 0 { 58 break 59 } 60 shift += 7 61 } 62 return value, sl 63 } 64 65 func writeUleb128(sb *strings.Builder, v uint64) { 66 if v < 128 { 67 sb.WriteByte(uint8(v)) 68 return 69 } 70 more := true 71 for more { 72 c := uint8(v & 0x7f) 73 v >>= 7 74 more = v != 0 75 if more { 76 c |= 0x80 77 } 78 sb.WriteByte(c) 79 } 80 }