1 // Copyright 2014 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.
12 // For gccgo, use go:linkname to export compiler-called functions.
14 //go:linkname concatstrings
15 //go:linkname slicebytetostring
16 //go:linkname slicebytetostringtmp
17 //go:linkname stringtoslicebyte
18 //go:linkname stringtoslicerune
19 //go:linkname slicerunetostring
20 //go:linkname intstring
21 // Temporary for C code to call:
22 //go:linkname gostringnocopy
23 //go:linkname findnull
25 // The constant is known to the compiler.
26 // There is no fundamental theory behind this number.
27 const tmpStringBufSize = 32
29 type tmpBuf [tmpStringBufSize]byte
31 // concatstrings implements a Go string concatenation x+y+z+...
32 // The operands are passed in the slice a.
33 // If buf != nil, the compiler has determined that the result does not
34 // escape the calling function, so the string data can be stored in buf
36 func concatstrings(buf *tmpBuf, p *string, n int) string {
38 *(*slice)(unsafe.Pointer(&a)) = slice{unsafe.Pointer(p), n, n}
48 throw("string concatenation too long")
58 // If there is just one string and either it is not on the stack
59 // or our result does not escape the calling frame (buf != nil),
60 // then we can return that string directly.
61 // Commented out for gccgo--no implementation of stringDataOnStack.
62 // if count == 1 && (buf != nil || !stringDataOnStack(a[idx])) {
65 s, b := rawstringtmp(buf, l)
73 // Buf is a fixed-size buffer for the result,
74 // it is not nil if the result does not escape.
75 func slicebytetostring(buf *tmpBuf, b []byte) (str string) {
78 // Turns out to be a relatively common case.
79 // Consider that you want to parse out data between parens in "foo()bar",
80 // you find the indices and convert the subslice to string.
84 racereadrangepc(unsafe.Pointer(&b[0]),
87 funcPC(slicebytetostring))
90 msanread(unsafe.Pointer(&b[0]), uintptr(l))
93 stringStructOf(&str).str = unsafe.Pointer(&staticbytes[b[0]])
94 stringStructOf(&str).len = 1
99 if buf != nil && len(b) <= len(buf) {
100 p = unsafe.Pointer(buf)
102 p = mallocgc(uintptr(len(b)), nil, false)
104 stringStructOf(&str).str = p
105 stringStructOf(&str).len = len(b)
106 memmove(p, (*(*slice)(unsafe.Pointer(&b))).array, uintptr(len(b)))
110 func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
111 if buf != nil && l <= len(buf) {
113 s = slicebytetostringtmp(b)
120 // slicebytetostringtmp returns a "string" referring to the actual []byte bytes.
122 // Callers need to ensure that the returned string will not be used after
123 // the calling goroutine modifies the original slice or synchronizes with
124 // another goroutine.
126 // The function is only called when instrumenting
127 // and otherwise intrinsified by the compiler.
129 // Some internal compiler optimizations use this function.
130 // - Used for m[T1{... Tn{..., string(k), ...} ...}] and m[string(k)]
131 // where k is []byte, T1 to Tn is a nesting of struct and array literals.
132 // - Used for "<"+string(b)+">" concatenation where b is []byte.
133 // - Used for string(b)=="foo" comparison where b is []byte.
134 func slicebytetostringtmp(b []byte) string {
135 if raceenabled && len(b) > 0 {
136 racereadrangepc(unsafe.Pointer(&b[0]),
139 funcPC(slicebytetostringtmp))
141 if msanenabled && len(b) > 0 {
142 msanread(unsafe.Pointer(&b[0]), uintptr(len(b)))
144 return *(*string)(unsafe.Pointer(&b))
147 func stringtoslicebyte(buf *tmpBuf, s string) []byte {
149 if buf != nil && len(s) <= len(buf) {
153 b = rawbyteslice(len(s))
159 func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
161 // unlike slicerunetostring, no race because strings are immutable.
168 if buf != nil && n <= len(buf) {
169 *buf = [tmpStringBufSize]rune{}
176 for _, r := range s {
183 func slicerunetostring(buf *tmpBuf, a []rune) string {
184 if raceenabled && len(a) > 0 {
185 racereadrangepc(unsafe.Pointer(&a[0]),
186 uintptr(len(a))*unsafe.Sizeof(a[0]),
188 funcPC(slicerunetostring))
190 if msanenabled && len(a) > 0 {
191 msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
195 for _, r := range a {
196 size1 += encoderune(dum[:], r)
198 s, b := rawstringtmp(buf, size1+3)
200 for _, r := range a {
205 size2 += encoderune(b[size2:], r)
210 type stringStruct struct {
215 // Variant with *byte pointer type for DWARF debugging.
216 type stringStructDWARF struct {
221 func stringStructOf(sp *string) *stringStruct {
222 return (*stringStruct)(unsafe.Pointer(sp))
225 func intstring(buf *[4]byte, v int64) (s string) {
226 if v >= 0 && v < runeSelf {
227 stringStructOf(&s).str = unsafe.Pointer(&staticbytes[v])
228 stringStructOf(&s).len = 1
235 s = slicebytetostringtmp(b)
239 if int64(rune(v)) != v {
242 n := encoderune(b, rune(v))
246 // rawstring allocates storage for a new string. The returned
247 // string and byte slice both refer to the same storage.
248 // The storage is not zeroed. Callers should use
249 // b to set the string contents and then drop b.
250 func rawstring(size int) (s string, b []byte) {
251 p := mallocgc(uintptr(size), nil, false)
253 stringStructOf(&s).str = p
254 stringStructOf(&s).len = size
256 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
261 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
262 func rawbyteslice(size int) (b []byte) {
263 cap := roundupsize(uintptr(size))
264 p := mallocgc(cap, nil, false)
265 if cap != uintptr(size) {
266 memclrNoHeapPointers(add(p, uintptr(size)), cap-uintptr(size))
269 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
273 // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
274 func rawruneslice(size int) (b []rune) {
275 if uintptr(size) > maxAlloc/4 {
276 throw("out of memory")
278 mem := roundupsize(uintptr(size) * 4)
279 p := mallocgc(mem, nil, false)
280 if mem != uintptr(size)*4 {
281 memclrNoHeapPointers(add(p, uintptr(size)*4), mem-uintptr(size)*4)
284 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
289 func gobytes(p *byte, n int) (b []byte) {
291 return make([]byte, 0)
294 if n < 0 || uintptr(n) > maxAlloc {
295 panic(errorString("gobytes: length out of range"))
298 bp := mallocgc(uintptr(n), nil, false)
299 memmove(bp, unsafe.Pointer(p), uintptr(n))
301 *(*slice)(unsafe.Pointer(&b)) = slice{bp, n, n}
305 // This is exported via linkname to assembly in syscall (for Plan9).
306 //go:linkname gostring
307 func gostring(p *byte) string {
313 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
317 func gostringn(p *byte, l int) string {
322 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
326 func index(s, t string) int {
330 for i := 0; i < len(s); i++ {
331 if s[i] == t[0] && hasPrefix(s[i:], t) {
338 func contains(s, t string) bool {
339 return index(s, t) >= 0
342 func hasPrefix(s, prefix string) bool {
343 return len(s) >= len(prefix) && s[:len(prefix)] == prefix
346 func hasSuffix(s, suffix string) bool {
347 return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
352 maxInt = int(maxUint >> 1)
355 // atoi parses an int from a string s.
356 // The bool result reports whether s is a number
357 // representable by a value of type int.
358 func atoi(s string) (int, bool) {
370 for i := 0; i < len(s); i++ {
372 if c < '0' || c > '9' {
380 un1 := un + uint(c) - '0'
388 if !neg && un > uint(maxInt) {
391 if neg && un > uint(maxInt)+1 {
403 // atoi32 is like atoi but for integers
404 // that fit into an int32.
405 func atoi32(s string) (int32, bool) {
406 if n, ok := atoi(s); n == int(int32(n)) {
413 func findnull(s *byte) int {
418 // Avoid IndexByteString on Plan 9 because it uses SSE instructions
419 // on x86 machines, and those are classified as floating point instructions,
420 // which are illegal in a note handler.
422 p := (*[maxAlloc/2 - 1]byte)(unsafe.Pointer(s))
430 // pageSize is the unit we scan at a time looking for NULL.
431 // It must be the minimum page size for any architecture Go
432 // runs on. It's okay (just a minor performance loss) if the
433 // actual system page size is larger than this value.
434 const pageSize = 4096
437 ptr := unsafe.Pointer(s)
438 // IndexByteString uses wide reads, so we need to be careful
439 // with page boundaries. Call IndexByteString on
440 // [ptr, endOfPage) interval.
441 safeLen := int(pageSize - uintptr(ptr)%pageSize)
444 t := *(*string)(unsafe.Pointer(&stringStruct{ptr, safeLen}))
445 // Check one page at a time.
446 if i := bytealg.IndexByteString(t, 0); i != -1 {
450 ptr = unsafe.Pointer(uintptr(ptr) + uintptr(safeLen))
456 func findnullw(s *uint16) int {
460 p := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(s))
469 func gostringnocopy(str *byte) string {
470 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
471 s := *(*string)(unsafe.Pointer(&ss))
475 func gostringw(strw *uint16) string {
477 str := (*[maxAlloc/2/2 - 1]uint16)(unsafe.Pointer(strw))
479 for i := 0; str[i] != 0; i++ {
480 n1 += encoderune(buf[:], rune(str[i]))
482 s, b := rawstring(n1 + 4)
484 for i := 0; str[i] != 0; i++ {
489 n2 += encoderune(b[n2:], rune(str[i]))
491 b[n2] = 0 // for luck
495 // These two functions are called by code generated by cgo -gccgo.
497 //go:linkname __go_byte_array_to_string __go_byte_array_to_string
498 func __go_byte_array_to_string(p unsafe.Pointer, l int) string {
502 s, c := rawstringtmp(nil, l)
503 memmove(unsafe.Pointer(&c[0]), p, uintptr(l))
507 //go:linkname __go_string_to_byte_array __go_string_to_byte_array
508 func __go_string_to_byte_array(s string) []byte {
509 return stringtoslicebyte(nil, s)
512 // parseRelease parses a dot-separated version number. It follows the
513 // semver syntax, but allows the minor and patch versions to be
515 func parseRelease(rel string) (major, minor, patch int, ok bool) {
516 // Strip anything after a dash or plus.
517 for i := 0; i < len(rel); i++ {
518 if rel[i] == '-' || rel[i] == '+' {
524 next := func() (int, bool) {
525 for i := 0; i < len(rel); i++ {
527 ver, ok := atoi(rel[:i])
536 if major, ok = next(); !ok || rel == "" {
539 if minor, ok = next(); !ok || rel == "" {