re PR lto/48086 (bootstrap-lto creates c-common.s with too many sections on x86_64...
[official-gcc.git] / libgo / go / gob / encode.go
blobd286a7e00b879b9666881cf0cab0f54e9cb9424d
1 // Copyright 2009 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 gob
7 import (
8 "bytes"
9 "io"
10 "math"
11 "os"
12 "reflect"
13 "unsafe"
16 const uint64Size = unsafe.Sizeof(uint64(0))
18 // The global execution state of an instance of the encoder.
19 // Field numbers are delta encoded and always increase. The field
20 // number is initialized to -1 so 0 comes out as delta(1). A delta of
21 // 0 terminates the structure.
22 type encoderState struct {
23 enc *Encoder
24 b *bytes.Buffer
25 sendZero bool // encoding an array element or map key/value pair; send zero values
26 fieldnum int // the last field number written.
27 buf [1 + uint64Size]byte // buffer used by the encoder; here to avoid allocation.
30 func newEncoderState(enc *Encoder, b *bytes.Buffer) *encoderState {
31 return &encoderState{enc: enc, b: b}
34 // Unsigned integers have a two-state encoding. If the number is less
35 // than 128 (0 through 0x7F), its value is written directly.
36 // Otherwise the value is written in big-endian byte order preceded
37 // by the byte length, negated.
39 // encodeUint writes an encoded unsigned integer to state.b.
40 func (state *encoderState) encodeUint(x uint64) {
41 if x <= 0x7F {
42 err := state.b.WriteByte(uint8(x))
43 if err != nil {
44 error(err)
46 return
48 var n, m int
49 m = uint64Size
50 for n = 1; x > 0; n++ {
51 state.buf[m] = uint8(x & 0xFF)
52 x >>= 8
53 m--
55 state.buf[m] = uint8(-(n - 1))
56 n, err := state.b.Write(state.buf[m : uint64Size+1])
57 if err != nil {
58 error(err)
62 // encodeInt writes an encoded signed integer to state.w.
63 // The low bit of the encoding says whether to bit complement the (other bits of the)
64 // uint to recover the int.
65 func (state *encoderState) encodeInt(i int64) {
66 var x uint64
67 if i < 0 {
68 x = uint64(^i<<1) | 1
69 } else {
70 x = uint64(i << 1)
72 state.encodeUint(uint64(x))
75 type encOp func(i *encInstr, state *encoderState, p unsafe.Pointer)
77 // The 'instructions' of the encoding machine
78 type encInstr struct {
79 op encOp
80 field int // field number
81 indir int // how many pointer indirections to reach the value in the struct
82 offset uintptr // offset in the structure of the field to encode
85 // Emit a field number and update the state to record its value for delta encoding.
86 // If the instruction pointer is nil, do nothing
87 func (state *encoderState) update(instr *encInstr) {
88 if instr != nil {
89 state.encodeUint(uint64(instr.field - state.fieldnum))
90 state.fieldnum = instr.field
94 // Each encoder is responsible for handling any indirections associated
95 // with the data structure. If any pointer so reached is nil, no bytes are written.
96 // If the data item is zero, no bytes are written.
97 // Otherwise, the output (for a scalar) is the field number, as an encoded integer,
98 // followed by the field data in its appropriate format.
100 func encIndirect(p unsafe.Pointer, indir int) unsafe.Pointer {
101 for ; indir > 0; indir-- {
102 p = *(*unsafe.Pointer)(p)
103 if p == nil {
104 return unsafe.Pointer(nil)
107 return p
110 func encBool(i *encInstr, state *encoderState, p unsafe.Pointer) {
111 b := *(*bool)(p)
112 if b || state.sendZero {
113 state.update(i)
114 if b {
115 state.encodeUint(1)
116 } else {
117 state.encodeUint(0)
122 func encInt(i *encInstr, state *encoderState, p unsafe.Pointer) {
123 v := int64(*(*int)(p))
124 if v != 0 || state.sendZero {
125 state.update(i)
126 state.encodeInt(v)
130 func encUint(i *encInstr, state *encoderState, p unsafe.Pointer) {
131 v := uint64(*(*uint)(p))
132 if v != 0 || state.sendZero {
133 state.update(i)
134 state.encodeUint(v)
138 func encInt8(i *encInstr, state *encoderState, p unsafe.Pointer) {
139 v := int64(*(*int8)(p))
140 if v != 0 || state.sendZero {
141 state.update(i)
142 state.encodeInt(v)
146 func encUint8(i *encInstr, state *encoderState, p unsafe.Pointer) {
147 v := uint64(*(*uint8)(p))
148 if v != 0 || state.sendZero {
149 state.update(i)
150 state.encodeUint(v)
154 func encInt16(i *encInstr, state *encoderState, p unsafe.Pointer) {
155 v := int64(*(*int16)(p))
156 if v != 0 || state.sendZero {
157 state.update(i)
158 state.encodeInt(v)
162 func encUint16(i *encInstr, state *encoderState, p unsafe.Pointer) {
163 v := uint64(*(*uint16)(p))
164 if v != 0 || state.sendZero {
165 state.update(i)
166 state.encodeUint(v)
170 func encInt32(i *encInstr, state *encoderState, p unsafe.Pointer) {
171 v := int64(*(*int32)(p))
172 if v != 0 || state.sendZero {
173 state.update(i)
174 state.encodeInt(v)
178 func encUint32(i *encInstr, state *encoderState, p unsafe.Pointer) {
179 v := uint64(*(*uint32)(p))
180 if v != 0 || state.sendZero {
181 state.update(i)
182 state.encodeUint(v)
186 func encInt64(i *encInstr, state *encoderState, p unsafe.Pointer) {
187 v := *(*int64)(p)
188 if v != 0 || state.sendZero {
189 state.update(i)
190 state.encodeInt(v)
194 func encUint64(i *encInstr, state *encoderState, p unsafe.Pointer) {
195 v := *(*uint64)(p)
196 if v != 0 || state.sendZero {
197 state.update(i)
198 state.encodeUint(v)
202 func encUintptr(i *encInstr, state *encoderState, p unsafe.Pointer) {
203 v := uint64(*(*uintptr)(p))
204 if v != 0 || state.sendZero {
205 state.update(i)
206 state.encodeUint(v)
210 // Floating-point numbers are transmitted as uint64s holding the bits
211 // of the underlying representation. They are sent byte-reversed, with
212 // the exponent end coming out first, so integer floating point numbers
213 // (for example) transmit more compactly. This routine does the
214 // swizzling.
215 func floatBits(f float64) uint64 {
216 u := math.Float64bits(f)
217 var v uint64
218 for i := 0; i < 8; i++ {
219 v <<= 8
220 v |= u & 0xFF
221 u >>= 8
223 return v
226 func encFloat32(i *encInstr, state *encoderState, p unsafe.Pointer) {
227 f := *(*float32)(p)
228 if f != 0 || state.sendZero {
229 v := floatBits(float64(f))
230 state.update(i)
231 state.encodeUint(v)
235 func encFloat64(i *encInstr, state *encoderState, p unsafe.Pointer) {
236 f := *(*float64)(p)
237 if f != 0 || state.sendZero {
238 state.update(i)
239 v := floatBits(f)
240 state.encodeUint(v)
244 // Complex numbers are just a pair of floating-point numbers, real part first.
245 func encComplex64(i *encInstr, state *encoderState, p unsafe.Pointer) {
246 c := *(*complex64)(p)
247 if c != 0+0i || state.sendZero {
248 rpart := floatBits(float64(real(c)))
249 ipart := floatBits(float64(imag(c)))
250 state.update(i)
251 state.encodeUint(rpart)
252 state.encodeUint(ipart)
256 func encComplex128(i *encInstr, state *encoderState, p unsafe.Pointer) {
257 c := *(*complex128)(p)
258 if c != 0+0i || state.sendZero {
259 rpart := floatBits(real(c))
260 ipart := floatBits(imag(c))
261 state.update(i)
262 state.encodeUint(rpart)
263 state.encodeUint(ipart)
267 func encNoOp(i *encInstr, state *encoderState, p unsafe.Pointer) {
270 // Byte arrays are encoded as an unsigned count followed by the raw bytes.
271 func encUint8Array(i *encInstr, state *encoderState, p unsafe.Pointer) {
272 b := *(*[]byte)(p)
273 if len(b) > 0 || state.sendZero {
274 state.update(i)
275 state.encodeUint(uint64(len(b)))
276 state.b.Write(b)
280 // Strings are encoded as an unsigned count followed by the raw bytes.
281 func encString(i *encInstr, state *encoderState, p unsafe.Pointer) {
282 s := *(*string)(p)
283 if len(s) > 0 || state.sendZero {
284 state.update(i)
285 state.encodeUint(uint64(len(s)))
286 io.WriteString(state.b, s)
290 // The end of a struct is marked by a delta field number of 0.
291 func encStructTerminator(i *encInstr, state *encoderState, p unsafe.Pointer) {
292 state.encodeUint(0)
295 // Execution engine
297 // The encoder engine is an array of instructions indexed by field number of the encoding
298 // data, typically a struct. It is executed top to bottom, walking the struct.
299 type encEngine struct {
300 instr []encInstr
303 const singletonField = 0
305 func (enc *Encoder) encodeSingle(b *bytes.Buffer, engine *encEngine, basep uintptr) {
306 state := newEncoderState(enc, b)
307 state.fieldnum = singletonField
308 // There is no surrounding struct to frame the transmission, so we must
309 // generate data even if the item is zero. To do this, set sendZero.
310 state.sendZero = true
311 instr := &engine.instr[singletonField]
312 p := unsafe.Pointer(basep) // offset will be zero
313 if instr.indir > 0 {
314 if p = encIndirect(p, instr.indir); p == nil {
315 return
318 instr.op(instr, state, p)
321 func (enc *Encoder) encodeStruct(b *bytes.Buffer, engine *encEngine, basep uintptr) {
322 state := newEncoderState(enc, b)
323 state.fieldnum = -1
324 for i := 0; i < len(engine.instr); i++ {
325 instr := &engine.instr[i]
326 p := unsafe.Pointer(basep + instr.offset)
327 if instr.indir > 0 {
328 if p = encIndirect(p, instr.indir); p == nil {
329 continue
332 instr.op(instr, state, p)
336 func (enc *Encoder) encodeArray(b *bytes.Buffer, p uintptr, op encOp, elemWid uintptr, elemIndir int, length int) {
337 state := newEncoderState(enc, b)
338 state.fieldnum = -1
339 state.sendZero = true
340 state.encodeUint(uint64(length))
341 for i := 0; i < length; i++ {
342 elemp := p
343 up := unsafe.Pointer(elemp)
344 if elemIndir > 0 {
345 if up = encIndirect(up, elemIndir); up == nil {
346 errorf("gob: encodeArray: nil element")
348 elemp = uintptr(up)
350 op(nil, state, unsafe.Pointer(elemp))
351 p += uintptr(elemWid)
355 func encodeReflectValue(state *encoderState, v reflect.Value, op encOp, indir int) {
356 for i := 0; i < indir && v != nil; i++ {
357 v = reflect.Indirect(v)
359 if v == nil {
360 errorf("gob: encodeReflectValue: nil element")
362 op(nil, state, unsafe.Pointer(v.Addr()))
365 func (enc *Encoder) encodeMap(b *bytes.Buffer, mv *reflect.MapValue, keyOp, elemOp encOp, keyIndir, elemIndir int) {
366 state := newEncoderState(enc, b)
367 state.fieldnum = -1
368 state.sendZero = true
369 keys := mv.Keys()
370 state.encodeUint(uint64(len(keys)))
371 for _, key := range keys {
372 encodeReflectValue(state, key, keyOp, keyIndir)
373 encodeReflectValue(state, mv.Elem(key), elemOp, elemIndir)
377 // To send an interface, we send a string identifying the concrete type, followed
378 // by the type identifier (which might require defining that type right now), followed
379 // by the concrete value. A nil value gets sent as the empty string for the name,
380 // followed by no value.
381 func (enc *Encoder) encodeInterface(b *bytes.Buffer, iv *reflect.InterfaceValue) {
382 state := newEncoderState(enc, b)
383 state.fieldnum = -1
384 state.sendZero = true
385 if iv.IsNil() {
386 state.encodeUint(0)
387 return
390 typ, _ := indirect(iv.Elem().Type())
391 name, ok := concreteTypeToName[typ]
392 if !ok {
393 errorf("gob: type not registered for interface: %s", typ)
395 // Send the name.
396 state.encodeUint(uint64(len(name)))
397 _, err := io.WriteString(state.b, name)
398 if err != nil {
399 error(err)
401 // Send (and maybe first define) the type id.
402 enc.sendTypeDescriptor(typ)
403 // Encode the value into a new buffer.
404 data := new(bytes.Buffer)
405 err = enc.encode(data, iv.Elem())
406 if err != nil {
407 error(err)
409 state.encodeUint(uint64(data.Len()))
410 _, err = state.b.Write(data.Bytes())
411 if err != nil {
412 error(err)
416 var encOpMap = []encOp{
417 reflect.Bool: encBool,
418 reflect.Int: encInt,
419 reflect.Int8: encInt8,
420 reflect.Int16: encInt16,
421 reflect.Int32: encInt32,
422 reflect.Int64: encInt64,
423 reflect.Uint: encUint,
424 reflect.Uint8: encUint8,
425 reflect.Uint16: encUint16,
426 reflect.Uint32: encUint32,
427 reflect.Uint64: encUint64,
428 reflect.Uintptr: encUintptr,
429 reflect.Float32: encFloat32,
430 reflect.Float64: encFloat64,
431 reflect.Complex64: encComplex64,
432 reflect.Complex128: encComplex128,
433 reflect.String: encString,
436 // Return the encoding op for the base type under rt and
437 // the indirection count to reach it.
438 func (enc *Encoder) encOpFor(rt reflect.Type) (encOp, int) {
439 typ, indir := indirect(rt)
440 var op encOp
441 k := typ.Kind()
442 if int(k) < len(encOpMap) {
443 op = encOpMap[k]
445 if op == nil {
446 // Special cases
447 switch t := typ.(type) {
448 case *reflect.SliceType:
449 if t.Elem().Kind() == reflect.Uint8 {
450 op = encUint8Array
451 break
453 // Slices have a header; we decode it to find the underlying array.
454 elemOp, indir := enc.encOpFor(t.Elem())
455 op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
456 slice := (*reflect.SliceHeader)(p)
457 if !state.sendZero && slice.Len == 0 {
458 return
460 state.update(i)
461 state.enc.encodeArray(state.b, slice.Data, elemOp, t.Elem().Size(), indir, int(slice.Len))
463 case *reflect.ArrayType:
464 // True arrays have size in the type.
465 elemOp, indir := enc.encOpFor(t.Elem())
466 op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
467 state.update(i)
468 state.enc.encodeArray(state.b, uintptr(p), elemOp, t.Elem().Size(), indir, t.Len())
470 case *reflect.MapType:
471 keyOp, keyIndir := enc.encOpFor(t.Key())
472 elemOp, elemIndir := enc.encOpFor(t.Elem())
473 op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
474 // Maps cannot be accessed by moving addresses around the way
475 // that slices etc. can. We must recover a full reflection value for
476 // the iteration.
477 v := reflect.NewValue(unsafe.Unreflect(t, unsafe.Pointer((p))))
478 mv := reflect.Indirect(v).(*reflect.MapValue)
479 if !state.sendZero && mv.Len() == 0 {
480 return
482 state.update(i)
483 state.enc.encodeMap(state.b, mv, keyOp, elemOp, keyIndir, elemIndir)
485 case *reflect.StructType:
486 // Generate a closure that calls out to the engine for the nested type.
487 enc.getEncEngine(typ)
488 info := mustGetTypeInfo(typ)
489 op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
490 state.update(i)
491 // indirect through info to delay evaluation for recursive structs
492 state.enc.encodeStruct(state.b, info.encoder, uintptr(p))
494 case *reflect.InterfaceType:
495 op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
496 // Interfaces transmit the name and contents of the concrete
497 // value they contain.
498 v := reflect.NewValue(unsafe.Unreflect(t, unsafe.Pointer((p))))
499 iv := reflect.Indirect(v).(*reflect.InterfaceValue)
500 if !state.sendZero && (iv == nil || iv.IsNil()) {
501 return
503 state.update(i)
504 state.enc.encodeInterface(state.b, iv)
508 if op == nil {
509 errorf("gob enc: can't happen: encode type %s", rt.String())
511 return op, indir
514 // The local Type was compiled from the actual value, so we know it's compatible.
515 func (enc *Encoder) compileEnc(rt reflect.Type) *encEngine {
516 srt, isStruct := rt.(*reflect.StructType)
517 engine := new(encEngine)
518 if isStruct {
519 engine.instr = make([]encInstr, srt.NumField()+1) // +1 for terminator
520 for fieldnum := 0; fieldnum < srt.NumField(); fieldnum++ {
521 f := srt.Field(fieldnum)
522 op, indir := enc.encOpFor(f.Type)
523 if !isExported(f.Name) {
524 op = encNoOp
526 engine.instr[fieldnum] = encInstr{op, fieldnum, indir, uintptr(f.Offset)}
528 engine.instr[srt.NumField()] = encInstr{encStructTerminator, 0, 0, 0}
529 } else {
530 engine.instr = make([]encInstr, 1)
531 op, indir := enc.encOpFor(rt)
532 engine.instr[0] = encInstr{op, singletonField, indir, 0} // offset is zero
534 return engine
537 // typeLock must be held (or we're in initialization and guaranteed single-threaded).
538 // The reflection type must have all its indirections processed out.
539 func (enc *Encoder) getEncEngine(rt reflect.Type) *encEngine {
540 info, err1 := getTypeInfo(rt)
541 if err1 != nil {
542 error(err1)
544 if info.encoder == nil {
545 // mark this engine as underway before compiling to handle recursive types.
546 info.encoder = new(encEngine)
547 info.encoder = enc.compileEnc(rt)
549 return info.encoder
552 // Put this in a function so we can hold the lock only while compiling, not when encoding.
553 func (enc *Encoder) lockAndGetEncEngine(rt reflect.Type) *encEngine {
554 typeLock.Lock()
555 defer typeLock.Unlock()
556 return enc.getEncEngine(rt)
559 func (enc *Encoder) encode(b *bytes.Buffer, value reflect.Value) (err os.Error) {
560 defer catchError(&err)
561 // Dereference down to the underlying object.
562 rt, indir := indirect(value.Type())
563 for i := 0; i < indir; i++ {
564 value = reflect.Indirect(value)
566 engine := enc.lockAndGetEncEngine(rt)
567 if value.Type().Kind() == reflect.Struct {
568 enc.encodeStruct(b, engine, value.Addr())
569 } else {
570 enc.encodeSingle(b, engine, value.Addr())
572 return nil