runtime: scan register backing store on ia64
[official-gcc.git] / libgo / go / runtime / string.go
blobe8df9a6b7c441d6c22079065185f6a612d243128
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.
5 package runtime
7 import "unsafe"
9 // For gccgo, use go:linkname to rename compiler-called functions to
10 // themselves, so that the compiler will export them.
12 //go:linkname concatstrings runtime.concatstrings
13 //go:linkname concatstring2 runtime.concatstring2
14 //go:linkname concatstring3 runtime.concatstring3
15 //go:linkname concatstring4 runtime.concatstring4
16 //go:linkname concatstring5 runtime.concatstring5
17 //go:linkname slicebytetostring runtime.slicebytetostring
18 //go:linkname slicebytetostringtmp runtime.slicebytetostringtmp
19 //go:linkname stringtoslicebyte runtime.stringtoslicebyte
20 //go:linkname stringtoslicerune runtime.stringtoslicerune
21 //go:linkname slicerunetostring runtime.slicerunetostring
22 //go:linkname intstring runtime.intstring
23 // Temporary for C code to call:
24 //go:linkname gostringnocopy runtime.gostringnocopy
25 //go:linkname findnull runtime.findnull
27 // The constant is known to the compiler.
28 // There is no fundamental theory behind this number.
29 const tmpStringBufSize = 32
31 type tmpBuf [tmpStringBufSize]byte
33 // concatstrings implements a Go string concatenation x+y+z+...
34 // The operands are passed in the slice a.
35 // If buf != nil, the compiler has determined that the result does not
36 // escape the calling function, so the string data can be stored in buf
37 // if small enough.
38 func concatstrings(buf *tmpBuf, a []string) string {
39 // idx := 0
40 l := 0
41 count := 0
42 for _, x := range a {
43 n := len(x)
44 if n == 0 {
45 continue
47 if l+n < l {
48 throw("string concatenation too long")
50 l += n
51 count++
52 // idx = i
54 if count == 0 {
55 return ""
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])) {
63 // return a[idx]
64 // }
65 s, b := rawstringtmp(buf, l)
66 for _, x := range a {
67 copy(b, x)
68 b = b[len(x):]
70 return s
73 func concatstring2(buf *tmpBuf, a [2]string) string {
74 return concatstrings(buf, a[:])
77 func concatstring3(buf *tmpBuf, a [3]string) string {
78 return concatstrings(buf, a[:])
81 func concatstring4(buf *tmpBuf, a [4]string) string {
82 return concatstrings(buf, a[:])
85 func concatstring5(buf *tmpBuf, a [5]string) string {
86 return concatstrings(buf, a[:])
89 // Buf is a fixed-size buffer for the result,
90 // it is not nil if the result does not escape.
91 func slicebytetostring(buf *tmpBuf, b []byte) (str string) {
92 l := len(b)
93 if l == 0 {
94 // Turns out to be a relatively common case.
95 // Consider that you want to parse out data between parens in "foo()bar",
96 // you find the indices and convert the subslice to string.
97 return ""
99 if raceenabled {
100 racereadrangepc(unsafe.Pointer(&b[0]),
101 uintptr(l),
102 getcallerpc(),
103 funcPC(slicebytetostring))
105 if msanenabled {
106 msanread(unsafe.Pointer(&b[0]), uintptr(l))
109 var p unsafe.Pointer
110 if buf != nil && len(b) <= len(buf) {
111 p = unsafe.Pointer(buf)
112 } else {
113 p = mallocgc(uintptr(len(b)), nil, false)
115 stringStructOf(&str).str = p
116 stringStructOf(&str).len = len(b)
117 memmove(p, (*(*slice)(unsafe.Pointer(&b))).array, uintptr(len(b)))
118 return
121 func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
122 if buf != nil && l <= len(buf) {
123 b = buf[:l]
124 s = slicebytetostringtmp(b)
125 } else {
126 s, b = rawstring(l)
128 return
131 // slicebytetostringtmp returns a "string" referring to the actual []byte bytes.
133 // Callers need to ensure that the returned string will not be used after
134 // the calling goroutine modifies the original slice or synchronizes with
135 // another goroutine.
137 // The function is only called when instrumenting
138 // and otherwise intrinsified by the compiler.
140 // Some internal compiler optimizations use this function.
141 // - Used for m[string(k)] lookup where m is a string-keyed map and k is a []byte.
142 // - Used for "<"+string(b)+">" concatenation where b is []byte.
143 // - Used for string(b)=="foo" comparison where b is []byte.
144 func slicebytetostringtmp(b []byte) string {
145 if raceenabled && len(b) > 0 {
146 racereadrangepc(unsafe.Pointer(&b[0]),
147 uintptr(len(b)),
148 getcallerpc(),
149 funcPC(slicebytetostringtmp))
151 if msanenabled && len(b) > 0 {
152 msanread(unsafe.Pointer(&b[0]), uintptr(len(b)))
154 return *(*string)(unsafe.Pointer(&b))
157 func stringtoslicebyte(buf *tmpBuf, s string) []byte {
158 var b []byte
159 if buf != nil && len(s) <= len(buf) {
160 *buf = tmpBuf{}
161 b = buf[:len(s)]
162 } else {
163 b = rawbyteslice(len(s))
165 copy(b, s)
166 return b
169 func stringtoslicerune(buf *[tmpStringBufSize]rune, s string) []rune {
170 // two passes.
171 // unlike slicerunetostring, no race because strings are immutable.
172 n := 0
173 for range s {
177 var a []rune
178 if buf != nil && n <= len(buf) {
179 *buf = [tmpStringBufSize]rune{}
180 a = buf[:n]
181 } else {
182 a = rawruneslice(n)
185 n = 0
186 for _, r := range s {
187 a[n] = r
190 return a
193 func slicerunetostring(buf *tmpBuf, a []rune) string {
194 if raceenabled && len(a) > 0 {
195 racereadrangepc(unsafe.Pointer(&a[0]),
196 uintptr(len(a))*unsafe.Sizeof(a[0]),
197 getcallerpc(),
198 funcPC(slicerunetostring))
200 if msanenabled && len(a) > 0 {
201 msanread(unsafe.Pointer(&a[0]), uintptr(len(a))*unsafe.Sizeof(a[0]))
203 var dum [4]byte
204 size1 := 0
205 for _, r := range a {
206 size1 += encoderune(dum[:], r)
208 s, b := rawstringtmp(buf, size1+3)
209 size2 := 0
210 for _, r := range a {
211 // check for race
212 if size2 >= size1 {
213 break
215 size2 += encoderune(b[size2:], r)
217 return s[:size2]
220 type stringStruct struct {
221 str unsafe.Pointer
222 len int
225 // Variant with *byte pointer type for DWARF debugging.
226 type stringStructDWARF struct {
227 str *byte
228 len int
231 func stringStructOf(sp *string) *stringStruct {
232 return (*stringStruct)(unsafe.Pointer(sp))
235 func intstring(buf *[4]byte, v int64) string {
236 var s string
237 var b []byte
238 if buf != nil {
239 b = buf[:]
240 s = slicebytetostringtmp(b)
241 } else {
242 s, b = rawstring(4)
244 if int64(rune(v)) != v {
245 v = runeError
247 n := encoderune(b, rune(v))
248 return s[:n]
251 // rawstring allocates storage for a new string. The returned
252 // string and byte slice both refer to the same storage.
253 // The storage is not zeroed. Callers should use
254 // b to set the string contents and then drop b.
255 func rawstring(size int) (s string, b []byte) {
256 p := mallocgc(uintptr(size), nil, false)
258 stringStructOf(&s).str = p
259 stringStructOf(&s).len = size
261 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
263 return
266 // rawbyteslice allocates a new byte slice. The byte slice is not zeroed.
267 func rawbyteslice(size int) (b []byte) {
268 cap := roundupsize(uintptr(size))
269 p := mallocgc(cap, nil, false)
270 if cap != uintptr(size) {
271 memclrNoHeapPointers(add(p, uintptr(size)), cap-uintptr(size))
274 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
275 return
278 // rawruneslice allocates a new rune slice. The rune slice is not zeroed.
279 func rawruneslice(size int) (b []rune) {
280 if uintptr(size) > _MaxMem/4 {
281 throw("out of memory")
283 mem := roundupsize(uintptr(size) * 4)
284 p := mallocgc(mem, nil, false)
285 if mem != uintptr(size)*4 {
286 memclrNoHeapPointers(add(p, uintptr(size)*4), mem-uintptr(size)*4)
289 *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(mem / 4)}
290 return
293 // used by cmd/cgo
294 func gobytes(p *byte, n int) []byte {
295 if n == 0 {
296 return make([]byte, 0)
298 x := make([]byte, n)
299 memmove(unsafe.Pointer(&x[0]), unsafe.Pointer(p), uintptr(n))
300 return x
303 func gostring(p *byte) string {
304 l := findnull(p)
305 if l == 0 {
306 return ""
308 s, b := rawstring(l)
309 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
310 return s
313 func gostringn(p *byte, l int) string {
314 if l == 0 {
315 return ""
317 s, b := rawstring(l)
318 memmove(unsafe.Pointer(&b[0]), unsafe.Pointer(p), uintptr(l))
319 return s
322 func index(s, t string) int {
323 if len(t) == 0 {
324 return 0
326 for i := 0; i < len(s); i++ {
327 if s[i] == t[0] && hasprefix(s[i:], t) {
328 return i
331 return -1
334 func contains(s, t string) bool {
335 return index(s, t) >= 0
338 func hasprefix(s, t string) bool {
339 return len(s) >= len(t) && s[:len(t)] == t
342 const (
343 maxUint = ^uint(0)
344 maxInt = int(maxUint >> 1)
347 // atoi parses an int from a string s.
348 // The bool result reports whether s is a number
349 // representable by a value of type int.
350 func atoi(s string) (int, bool) {
351 if s == "" {
352 return 0, false
355 neg := false
356 if s[0] == '-' {
357 neg = true
358 s = s[1:]
361 un := uint(0)
362 for i := 0; i < len(s); i++ {
363 c := s[i]
364 if c < '0' || c > '9' {
365 return 0, false
367 if un > maxUint/10 {
368 // overflow
369 return 0, false
371 un *= 10
372 un1 := un + uint(c) - '0'
373 if un1 < un {
374 // overflow
375 return 0, false
377 un = un1
380 if !neg && un > uint(maxInt) {
381 return 0, false
383 if neg && un > uint(maxInt)+1 {
384 return 0, false
387 n := int(un)
388 if neg {
389 n = -n
392 return n, true
395 // atoi32 is like atoi but for integers
396 // that fit into an int32.
397 func atoi32(s string) (int32, bool) {
398 if n, ok := atoi(s); n == int(int32(n)) {
399 return int32(n), ok
401 return 0, false
404 //go:nosplit
405 func findnull(s *byte) int {
406 if s == nil {
407 return 0
409 p := (*[_MaxMem/2 - 1]byte)(unsafe.Pointer(s))
410 l := 0
411 for p[l] != 0 {
414 return l
417 func findnullw(s *uint16) int {
418 if s == nil {
419 return 0
421 p := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(s))
422 l := 0
423 for p[l] != 0 {
426 return l
429 //go:nosplit
430 func gostringnocopy(str *byte) string {
431 ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
432 s := *(*string)(unsafe.Pointer(&ss))
433 return s
436 func gostringw(strw *uint16) string {
437 var buf [8]byte
438 str := (*[_MaxMem/2/2 - 1]uint16)(unsafe.Pointer(strw))
439 n1 := 0
440 for i := 0; str[i] != 0; i++ {
441 n1 += encoderune(buf[:], rune(str[i]))
443 s, b := rawstring(n1 + 4)
444 n2 := 0
445 for i := 0; str[i] != 0; i++ {
446 // check for race
447 if n2 >= n1 {
448 break
450 n2 += encoderune(b[n2:], rune(str[i]))
452 b[n2] = 0 // for luck
453 return s[:n2]
456 // These two functions are called by code generated by cgo -gccgo.
458 //go:linkname __go_byte_array_to_string __go_byte_array_to_string
459 func __go_byte_array_to_string(p unsafe.Pointer, l int) string {
460 if l == 0 {
461 return ""
463 s, c := rawstringtmp(nil, l)
464 memmove(unsafe.Pointer(&c[0]), p, uintptr(l))
465 return s
468 //go:linkname __go_string_to_byte_array __go_string_to_byte_array
469 func __go_string_to_byte_array(s string) []byte {
470 return stringtoslicebyte(nil, s)