return state;
}
-func encAddrOf(state *EncState, instr *encInstr) unsafe.Pointer {
- p := unsafe.Pointer(state.base+instr.offset);
- return encIndirect(p, instr.indir);
-}
-
// Test instruction execution for encoding.
// Do not run the machine yet; instead do individual instructions crafted by hand.
func TestScalarEncInstructions(t *testing.T) {
data := struct { a bool } { true };
instr := &encInstr{ encBool, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(boolResult, b.Data()) {
t.Errorf("bool enc instructions: expected % x got % x", boolResult, b.Data())
}
data := struct { a int } { 17 };
instr := &encInstr{ encInt, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int enc instructions: expected % x got % x", signedResult, b.Data())
}
data := struct { a uint } { 17 };
instr := &encInstr{ encUint, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint enc instructions: expected % x got % x", unsignedResult, b.Data())
}
data := struct { a int8 } { 17 };
instr := &encInstr{ encInt, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int8 enc instructions: expected % x got % x", signedResult, b.Data())
}
data := struct { a uint8 } { 17 };
instr := &encInstr{ encUint, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint8 enc instructions: expected % x got % x", unsignedResult, b.Data())
}
data := struct { a int16 } { 17 };
instr := &encInstr{ encInt16, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int16 enc instructions: expected % x got % x", signedResult, b.Data())
}
data := struct { a uint16 } { 17 };
instr := &encInstr{ encUint16, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint16 enc instructions: expected % x got % x", unsignedResult, b.Data())
}
data := struct { a int32 } { 17 };
instr := &encInstr{ encInt32, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int32 enc instructions: expected % x got % x", signedResult, b.Data())
}
data := struct { a uint32 } { 17 };
instr := &encInstr{ encUint32, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint32 enc instructions: expected % x got % x", unsignedResult, b.Data())
}
data := struct { a int64 } { 17 };
instr := &encInstr{ encInt64, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(signedResult, b.Data()) {
t.Errorf("int64 enc instructions: expected % x got % x", signedResult, b.Data())
}
data := struct { a uint64 } { 17 };
instr := &encInstr{ encUint, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(unsignedResult, b.Data()) {
t.Errorf("uint64 enc instructions: expected % x got % x", unsignedResult, b.Data())
}
data := struct { a float } { 17 };
instr := &encInstr{ encFloat, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(floatResult, b.Data()) {
t.Errorf("float enc instructions: expected % x got % x", floatResult, b.Data())
}
data := struct { a float32 } { 17 };
instr := &encInstr{ encFloat32, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(floatResult, b.Data()) {
t.Errorf("float32 enc instructions: expected % x got % x", floatResult, b.Data())
}
data := struct { a float64 } { 17 };
instr := &encInstr{ encFloat64, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(floatResult, b.Data()) {
t.Errorf("float64 enc instructions: expected % x got % x", floatResult, b.Data())
}
data := struct { a []byte } { strings.Bytes("hello") };
instr := &encInstr{ encUint8Array, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(bytesResult, b.Data()) {
t.Errorf("bytes enc instructions: expected % x got % x", bytesResult, b.Data())
}
data := struct { a string } { "hello" };
instr := &encInstr{ encString, 6, 0, 0 };
state := newEncState(b);
- state.base = uintptr(unsafe.Pointer(&data));
- instr.op(instr, state, encAddrOf(state, instr));
+ instr.op(instr, state, unsafe.Pointer(&data));
if !bytes.Equal(bytesResult, b.Data()) {
t.Errorf("string enc instructions: expected % x got % x", bytesResult, b.Data())
}
}
}
-// derive the address of a field, after indirecting indir times.
-func decAddrOf(state *DecState, instr *decInstr) unsafe.Pointer {
- p := unsafe.Pointer(state.base+instr.offset);
- return decIndirect(p, instr.indir);
-}
-
-func execDec(typ string, instr *decInstr, state *DecState, t *testing.T) {
+func execDec(typ string, instr *decInstr, state *DecState, t *testing.T, p unsafe.Pointer) {
v := int(DecodeUint(state));
if state.err != nil {
t.Fatalf("decoding %s field: %v", typ, state.err);
if v + state.fieldnum != 6 {
t.Fatalf("decoding field number %d, got %d", 6, v + state.fieldnum);
}
- instr.op(instr, state, decAddrOf(state, instr));
+ instr.op(instr, state, decIndirect(p, instr.indir));
state.fieldnum = 6;
}
var data struct { a bool };
instr := &decInstr{ decBool, 6, 0, 0 };
state := newDecState(boolResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("bool", instr, state, t);
+ execDec("bool", instr, state, t, unsafe.Pointer(&data));
if data.a != true {
t.Errorf("int a = %v not true", data.a)
}
var data struct { a int };
instr := &decInstr{ decInt, 6, 0, 0 };
state := newDecState(signedResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("int", instr, state, t);
+ execDec("int", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a uint };
instr := &decInstr{ decUint, 6, 0, 0 };
state := newDecState(unsignedResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("uint", instr, state, t);
+ execDec("uint", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a int8 };
instr := &decInstr{ decInt8, 6, 0, 0 };
state := newDecState(signedResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("int8", instr, state, t);
+ execDec("int8", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a uint8 };
instr := &decInstr{ decUint8, 6, 0, 0 };
state := newDecState(unsignedResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("uint8", instr, state, t);
+ execDec("uint8", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a int16 };
instr := &decInstr{ decInt16, 6, 0, 0 };
state := newDecState(signedResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("int16", instr, state, t);
+ execDec("int16", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a uint16 };
instr := &decInstr{ decUint16, 6, 0, 0 };
state := newDecState(unsignedResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("uint16", instr, state, t);
+ execDec("uint16", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a int32 };
instr := &decInstr{ decInt32, 6, 0, 0 };
state := newDecState(signedResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("int32", instr, state, t);
+ execDec("int32", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a uint32 };
instr := &decInstr{ decUint32, 6, 0, 0 };
state := newDecState(unsignedResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("uint32", instr, state, t);
+ execDec("uint32", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a int64 };
instr := &decInstr{ decInt64, 6, 0, 0 };
state := newDecState(signedResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("int64", instr, state, t);
+ execDec("int64", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a uint64 };
instr := &decInstr{ decUint64, 6, 0, 0 };
state := newDecState(unsignedResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("uint64", instr, state, t);
+ execDec("uint64", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a float };
instr := &decInstr{ decFloat, 6, 0, 0 };
state := newDecState(floatResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("float", instr, state, t);
+ execDec("float", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a float32 };
instr := &decInstr{ decFloat32, 6, 0, 0 };
state := newDecState(floatResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("float32", instr, state, t);
+ execDec("float32", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a float64 };
instr := &decInstr{ decFloat64, 6, 0, 0 };
state := newDecState(floatResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("float64", instr, state, t);
+ execDec("float64", instr, state, t, unsafe.Pointer(&data));
if data.a != 17 {
t.Errorf("int a = %v not 17", data.a)
}
var data struct { a []byte };
instr := &decInstr{ decUint8Array, 6, 0, 0 };
state := newDecState(bytesResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("bytes", instr, state, t);
+ execDec("bytes", instr, state, t, unsafe.Pointer(&data));
if string(data.a) != "hello" {
t.Errorf(`bytes a = %q not "hello"`, string(data.a))
}
var data struct { a string };
instr := &decInstr{ decString, 6, 0, 0 };
state := newDecState(bytesResult);
- state.base = uintptr(unsafe.Pointer(&data));
- execDec("bytes", instr, state, t);
+ execDec("bytes", instr, state, t, unsafe.Pointer(&data));
if data.a != "hello" {
t.Errorf(`bytes a = %q not "hello"`, data.a)
}
}
type T1 struct {
a, b,c int;
+ n *[3]float;
s string;
y []byte;
t *T2;
a: 17,
b: 18,
c: -5,
+ n: &[3]float{1.5, 2.5, 3.5},
s: "Now is the time",
y: strings.Bytes("hello, sailor"),
t: &T2{"this is T2"},
// 0 terminates the structure.
type EncState struct {
w io.Writer;
- base uintptr; // the base address of the data structure being written
err os.Error; // error encountered during encoding;
fieldnum int; // the last field number written.
buf [16]byte; // buffer used by the encoder; here to avoid allocation.
offset uintptr; // offset in the structure of the field to encode
}
+// Emit a field number and update the state to record its value for delta encoding.
+// If the instruction pointer is nil, do nothing
+func (state *EncState) update(instr *encInstr) {
+ if instr != nil {
+ EncodeUint(state, uint64(instr.field - state.fieldnum));
+ state.fieldnum = instr.field;
+ }
+}
+
// Each encoder is responsible for handling any indirections associated
// with the data structure. If any pointer so reached is nil, no bytes are written.
// If the data item is zero, no bytes are written.
func encBool(i *encInstr, state *EncState, p unsafe.Pointer) {
b := *(*bool)(p);
if b {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeUint(state, 1);
- state.fieldnum = i.field;
}
}
func encInt(i *encInstr, state *EncState, p unsafe.Pointer) {
v := int64(*(*int)(p));
if v != 0 {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeInt(state, v);
- state.fieldnum = i.field;
}
}
func encUint(i *encInstr, state *EncState, p unsafe.Pointer) {
v := uint64(*(*uint)(p));
if v != 0 {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeUint(state, v);
- state.fieldnum = i.field;
}
}
func encInt8(i *encInstr, state *EncState, p unsafe.Pointer) {
v := int64(*(*int8)(p));
if v != 0 {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeInt(state, v);
- state.fieldnum = i.field;
}
}
func encUint8(i *encInstr, state *EncState, p unsafe.Pointer) {
v := uint64(*(*uint8)(p));
if v != 0 {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeUint(state, v);
- state.fieldnum = i.field;
}
}
func encInt16(i *encInstr, state *EncState, p unsafe.Pointer) {
v := int64(*(*int16)(p));
if v != 0 {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeInt(state, v);
- state.fieldnum = i.field;
}
}
func encUint16(i *encInstr, state *EncState, p unsafe.Pointer) {
v := uint64(*(*uint16)(p));
if v != 0 {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeUint(state, v);
- state.fieldnum = i.field;
}
}
func encInt32(i *encInstr, state *EncState, p unsafe.Pointer) {
v := int64(*(*int32)(p));
if v != 0 {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeInt(state, v);
- state.fieldnum = i.field;
}
}
func encUint32(i *encInstr, state *EncState, p unsafe.Pointer) {
v := uint64(*(*uint32)(p));
if v != 0 {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeUint(state, v);
- state.fieldnum = i.field;
}
}
func encInt64(i *encInstr, state *EncState, p unsafe.Pointer) {
v := *(*int64)(p);
if v != 0 {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeInt(state, v);
- state.fieldnum = i.field;
}
}
func encUint64(i *encInstr, state *EncState, p unsafe.Pointer) {
v := *(*uint64)(p);
if v != 0 {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeUint(state, v);
- state.fieldnum = i.field;
}
}
f := float(*(*float)(p));
if f != 0 {
v := floatBits(float64(f));
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeUint(state, v);
- state.fieldnum = i.field;
}
}
f := float32(*(*float32)(p));
if f != 0 {
v := floatBits(float64(f));
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeUint(state, v);
- state.fieldnum = i.field;
}
}
func encFloat64(i *encInstr, state *EncState, p unsafe.Pointer) {
f := *(*float64)(p);
if f != 0 {
+ state.update(i);
v := floatBits(f);
- EncodeUint(state, uint64(i.field - state.fieldnum));
EncodeUint(state, v);
- state.fieldnum = i.field;
}
}
func encUint8Array(i *encInstr, state *EncState, p unsafe.Pointer) {
b := *(*[]byte)(p);
if len(b) > 0 {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeUint(state, uint64(len(b)));
state.w.Write(b);
- state.fieldnum = i.field;
}
}
func encString(i *encInstr, state *EncState, p unsafe.Pointer) {
s := *(*string)(p);
if len(s) > 0 {
- EncodeUint(state, uint64(i.field - state.fieldnum));
+ state.update(i);
EncodeUint(state, uint64(len(s)));
io.WriteString(state.w, s);
- state.fieldnum = i.field;
}
}
instr []encInstr
}
-func (engine *encEngine) encodeStruct(w io.Writer, p uintptr) os.Error {
+func encodeStruct(engine *encEngine, w io.Writer, basep uintptr) os.Error {
state := new(EncState);
state.w = w;
- state.base = p;
state.fieldnum = -1;
for i := 0; i < len(engine.instr); i++ {
instr := &engine.instr[i];
- p := unsafe.Pointer(state.base+instr.offset);
+ p := unsafe.Pointer(basep+instr.offset);
if instr.indir > 0 {
if p = encIndirect(p, instr.indir); p == nil {
state.fieldnum = i;
return state.err
}
+func encodeArray(w io.Writer, p uintptr, op encOp, elemWid int, length int) os.Error {
+ state := new(EncState);
+ state.w = w;
+ state.fieldnum = -1;
+ EncodeUint(state, uint64(length));
+ for i := 0; i < length && state.err == nil; i++ {
+ op(nil, state, unsafe.Pointer(p)); // TODO(r): indir on elements
+ p += uintptr(elemWid);
+ }
+ return state.err
+}
+
var encEngineMap = make(map[reflect.Type] *encEngine)
var encOpMap = map[int] encOp {
reflect.BoolKind: encBool,
// Special cases
if typ.Kind() == reflect.ArrayKind {
atyp := typ.(reflect.ArrayType);
- switch atyp.Elem().Kind() {
- case reflect.Uint8Kind:
+ switch {
+ case atyp.Elem().Kind() == reflect.Uint8Kind:
op = encUint8Array
+ case atyp.IsSlice():
+ // Slices have a header; we decode it to find the underlying array.
+ elemOp := encOpFor(atyp.Elem());
+ op = func(i *encInstr, state *EncState, p unsafe.Pointer) {
+ slice := *(*reflect.SliceHeader)(p);
+ if slice.Len == 0 {
+ return
+ }
+ state.update(i);
+ state.err = encodeArray(state.w, slice.Data, elemOp, atyp.Elem().Size(), int(slice.Len));
+ };
+ case !atyp.IsSlice():
+ // True arrays have size in the type.
+ elemOp := encOpFor(atyp.Elem());
+ op = func(i *encInstr, state *EncState, p unsafe.Pointer) {
+ state.update(i);
+ state.err = encodeArray(state.w, uintptr(p), elemOp, atyp.Elem().Size(), atyp.Len());
+ };
}
}
if typ.Kind() == reflect.StructKind {
// Generate a closure that calls out to the engine for the nested type.
engine := getEncEngine(typ);
op = func(i *encInstr, state *EncState, p unsafe.Pointer) {
- EncodeUint(state, uint64(i.field - state.fieldnum));
- state.err = engine.encodeStruct(state.w, uintptr(p));
- state.fieldnum = i.field;
+ state.update(i);
+ state.err = encodeStruct(engine, state.w, uintptr(p));
};
}
}
typeLock.Lock();
engine := getEncEngine(rt);
typeLock.Unlock();
- return engine.encodeStruct(w, uintptr(v.(reflect.StructValue).Addr()));
+ return encodeStruct(engine, w, uintptr(v.(reflect.StructValue).Addr()));
}