Commit 282493bb authored by Rob Pike's avatar Rob Pike

update reflection library to int/int32 etc. split

fmt still to come

R=rsc
DELTA=168  (141 added, 19 deleted, 8 changed)
OCL=18064
CL=18071
parent c5e7e196
...@@ -11,6 +11,16 @@ TEXT reflect·PtrAddrToAddr(SB),7,$-8 ...@@ -11,6 +11,16 @@ TEXT reflect·PtrAddrToAddr(SB),7,$-8
MOVQ AX, 16(SP) MOVQ AX, 16(SP)
RET RET
TEXT reflect·AddrToPtrInt(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
TEXT reflect·PtrIntToAddr(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
TEXT reflect·AddrToPtrInt8(SB),7,$-8 TEXT reflect·AddrToPtrInt8(SB),7,$-8
MOVQ 8(SP), AX MOVQ 8(SP), AX
MOVQ AX, 16(SP) MOVQ AX, 16(SP)
...@@ -51,6 +61,16 @@ TEXT reflect·PtrInt64ToAddr(SB),7,$-8 ...@@ -51,6 +61,16 @@ TEXT reflect·PtrInt64ToAddr(SB),7,$-8
MOVQ AX, 16(SP) MOVQ AX, 16(SP)
RET RET
TEXT reflect·AddrToPtrUint(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
TEXT reflect·PtrUintToAddr(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
TEXT reflect·AddrToPtrUint8(SB),7,$-8 TEXT reflect·AddrToPtrUint8(SB),7,$-8
MOVQ 8(SP), AX MOVQ 8(SP), AX
MOVQ AX, 16(SP) MOVQ AX, 16(SP)
...@@ -91,6 +111,16 @@ TEXT reflect·PtrUint64ToAddr(SB),7,$-8 ...@@ -91,6 +111,16 @@ TEXT reflect·PtrUint64ToAddr(SB),7,$-8
MOVQ AX, 16(SP) MOVQ AX, 16(SP)
RET RET
TEXT reflect·AddrToPtrFloat(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
TEXT reflect·PtrFloatToAddr(SB),7,$-8
MOVQ 8(SP), AX
MOVQ AX, 16(SP)
RET
TEXT reflect·AddrToPtrFloat32(SB),7,$-8 TEXT reflect·AddrToPtrFloat32(SB),7,$-8
MOVQ 8(SP), AX MOVQ 8(SP), AX
MOVQ AX, 16(SP) MOVQ AX, 16(SP)
......
...@@ -21,14 +21,17 @@ BEGIN { ...@@ -21,14 +21,17 @@ BEGIN {
} }
' > cast_$GOARCH.s << '!' ' > cast_$GOARCH.s << '!'
Addr Addr
Int
Int8 Int8
Int16 Int16
Int32 Int32
Int64 Int64
Uint
Uint8 Uint8
Uint16 Uint16
Uint32 Uint32
Uint64 Uint64
Float
Float32 Float32
Float64 Float64
Float80 Float80
......
...@@ -49,6 +49,8 @@ func valuedump(s, t string) { ...@@ -49,6 +49,8 @@ func valuedump(s, t string) {
typ := reflect.ParseTypeString("", s); typ := reflect.ParseTypeString("", s);
v := reflect.NewInitValue(typ); v := reflect.NewInitValue(typ);
switch v.Kind() { switch v.Kind() {
case reflect.IntKind:
v.(reflect.IntValue).Put(132);
case reflect.Int8Kind: case reflect.Int8Kind:
v.(reflect.Int8Value).Put(8); v.(reflect.Int8Value).Put(8);
case reflect.Int16Kind: case reflect.Int16Kind:
...@@ -57,6 +59,8 @@ func valuedump(s, t string) { ...@@ -57,6 +59,8 @@ func valuedump(s, t string) {
v.(reflect.Int32Value).Put(32); v.(reflect.Int32Value).Put(32);
case reflect.Int64Kind: case reflect.Int64Kind:
v.(reflect.Int64Value).Put(64); v.(reflect.Int64Value).Put(64);
case reflect.UintKind:
v.(reflect.UintValue).Put(132);
case reflect.Uint8Kind: case reflect.Uint8Kind:
v.(reflect.Uint8Value).Put(8); v.(reflect.Uint8Value).Put(8);
case reflect.Uint16Kind: case reflect.Uint16Kind:
...@@ -65,6 +69,8 @@ func valuedump(s, t string) { ...@@ -65,6 +69,8 @@ func valuedump(s, t string) {
v.(reflect.Uint32Value).Put(32); v.(reflect.Uint32Value).Put(32);
case reflect.Uint64Kind: case reflect.Uint64Kind:
v.(reflect.Uint64Value).Put(64); v.(reflect.Uint64Value).Put(64);
case reflect.FloatKind:
v.(reflect.FloatValue).Put(3200.0);
case reflect.Float32Kind: case reflect.Float32Kind:
v.(reflect.Float32Value).Put(32.0); v.(reflect.Float32Value).Put(32.0);
case reflect.Float64Kind: case reflect.Float64Kind:
...@@ -83,14 +89,17 @@ func main() { ...@@ -83,14 +89,17 @@ func main() {
var s string; var s string;
var t reflect.Type; var t reflect.Type;
typedump("int", "int");
typedump("int8", "int8"); typedump("int8", "int8");
typedump("int16", "int16"); typedump("int16", "int16");
typedump("int32", "int32"); typedump("int32", "int32");
typedump("int64", "int64"); typedump("int64", "int64");
typedump("uint", "uint");
typedump("uint8", "uint8"); typedump("uint8", "uint8");
typedump("uint16", "uint16"); typedump("uint16", "uint16");
typedump("uint32", "uint32"); typedump("uint32", "uint32");
typedump("uint64", "uint64"); typedump("uint64", "uint64");
typedump("float", "float");
typedump("float32", "float32"); typedump("float32", "float32");
typedump("float64", "float64"); typedump("float64", "float64");
typedump("float80", "float80"); typedump("float80", "float80");
...@@ -149,7 +158,7 @@ func main() { ...@@ -149,7 +158,7 @@ func main() {
var i int = 7; var i int = 7;
var tmp = &T{123, 456.0, "hello", &i}; var tmp = &T{123, 456.0, "hello", &i};
value := reflect.NewValue(tmp); value := reflect.NewValue(tmp);
assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.T{123, +4.560000e+02, hello, *int32(@)}"); assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.T{123, +4.560000e+02, hello, *int(@)}");
} }
{ {
type C chan *T; // TODO: should not be necessary type C chan *T; // TODO: should not be necessary
...@@ -162,7 +171,7 @@ func main() { ...@@ -162,7 +171,7 @@ func main() {
var tmp A = A{1,2,3,4,5,6,7,8,9,10}; var tmp A = A{1,2,3,4,5,6,7,8,9,10};
value := reflect.NewValue(&tmp); value := reflect.NewValue(&tmp);
assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.A_test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"); assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.A_test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.Int32Value).Put(123); value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Put(123);
assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.A_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"); assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.A_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
} }
{ {
...@@ -171,7 +180,7 @@ func main() { ...@@ -171,7 +180,7 @@ func main() {
var tmp *AA = &tmp1; var tmp *AA = &tmp1;
value := reflect.NewValue(tmp); value := reflect.NewValue(tmp);
assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.AA_test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"); assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.AA_test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.Int32Value).Put(123); value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Put(123);
assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.AA_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"); assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.AA_test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
} }
} }
...@@ -41,30 +41,11 @@ func TypeToString(typ Type, expand bool) string { ...@@ -41,30 +41,11 @@ func TypeToString(typ Type, expand bool) string {
switch(typ.Kind()) { switch(typ.Kind()) {
case MissingKind: case MissingKind:
return "$missing$"; return "$missing$";
case Int8Kind: case IntKind, Int8Kind, Int16Kind, Int32Kind, Int64Kind,
return "int8"; UintKind, Uint8Kind, Uint16Kind, Uint32Kind, Uint64Kind,
case Int16Kind: FloatKind, Float32Kind, Float64Kind, Float80Kind:
return "int16"; StringKind:
case Int32Kind: return typ.Name();
return "int32";
case Int64Kind:
return "int64";
case Uint8Kind:
return "uint8";
case Uint16Kind:
return "uint16";
case Uint32Kind:
return "uint32";
case Uint64Kind:
return "uint64";
case Float32Kind:
return "float32";
case Float64Kind:
return "float64";
case Float80Kind:
return "float80";
case StringKind:
return "string";
case PtrKind: case PtrKind:
p := typ.(PtrType); p := typ.(PtrType);
return "*" + TypeToString(p.Sub(), false); return "*" + TypeToString(p.Sub(), false);
...@@ -125,6 +106,8 @@ func ValueToString(val Value) string { ...@@ -125,6 +106,8 @@ func ValueToString(val Value) string {
switch(val.Kind()) { switch(val.Kind()) {
case MissingKind: case MissingKind:
return "missing"; return "missing";
case IntKind:
return integer(int64(val.(IntValue).Get()));
case Int8Kind: case Int8Kind:
return integer(int64(val.(Int8Value).Get())); return integer(int64(val.(Int8Value).Get()));
case Int16Kind: case Int16Kind:
...@@ -133,6 +116,8 @@ func ValueToString(val Value) string { ...@@ -133,6 +116,8 @@ func ValueToString(val Value) string {
return integer(int64(val.(Int32Value).Get())); return integer(int64(val.(Int32Value).Get()));
case Int64Kind: case Int64Kind:
return integer(int64(val.(Int64Value).Get())); return integer(int64(val.(Int64Value).Get()));
case UintKind:
return integer(int64(val.(UintValue).Get()));
case Uint8Kind: case Uint8Kind:
return integer(int64(val.(Uint8Value).Get())); return integer(int64(val.(Uint8Value).Get()));
case Uint16Kind: case Uint16Kind:
...@@ -141,6 +126,8 @@ func ValueToString(val Value) string { ...@@ -141,6 +126,8 @@ func ValueToString(val Value) string {
return integer(int64(val.(Uint32Value).Get())); return integer(int64(val.(Uint32Value).Get()));
case Uint64Kind: case Uint64Kind:
return integer(int64(val.(Uint64Value).Get())); return integer(int64(val.(Uint64Value).Get()));
case FloatKind:
return floatingpoint(float64(val.(FloatValue).Get()));
case Float32Kind: case Float32Kind:
return floatingpoint(float64(val.(Float32Value).Get())); return floatingpoint(float64(val.(Float32Value).Get()));
case Float64Kind: case Float64Kind:
......
...@@ -17,10 +17,12 @@ export const ( ...@@ -17,10 +17,12 @@ export const (
MissingKind = iota; MissingKind = iota;
ArrayKind; ArrayKind;
ChanKind; ChanKind;
FloatKind;
Float32Kind; Float32Kind;
Float64Kind; Float64Kind;
Float80Kind; Float80Kind;
FuncKind; FuncKind;
IntKind;
Int16Kind; Int16Kind;
Int32Kind; Int32Kind;
Int64Kind; Int64Kind;
...@@ -30,6 +32,7 @@ export const ( ...@@ -30,6 +32,7 @@ export const (
PtrKind; PtrKind;
StringKind; StringKind;
StructKind; StructKind;
UintKind;
Uint16Kind; Uint16Kind;
Uint32Kind; Uint32Kind;
Uint64Kind; Uint64Kind;
...@@ -79,14 +82,17 @@ func NewBasicType(name string, kind int, size uint64) Type { ...@@ -79,14 +82,17 @@ func NewBasicType(name string, kind int, size uint64) Type {
// Prebuilt basic types // Prebuilt basic types
export var ( export var (
Missing = NewBasicType(MissingString, MissingKind, 1); Missing = NewBasicType(MissingString, MissingKind, 1);
Int = NewBasicType("int", IntKind, 4); // TODO: need to know how big an int is
Int8 = NewBasicType("int8", Int8Kind, 1); Int8 = NewBasicType("int8", Int8Kind, 1);
Int16 = NewBasicType("int16", Int16Kind, 2); Int16 = NewBasicType("int16", Int16Kind, 2);
Int32 = NewBasicType("int32", Int32Kind, 4); Int32 = NewBasicType("int32", Int32Kind, 4);
Int64 = NewBasicType("int64", Int64Kind, 8); Int64 = NewBasicType("int64", Int64Kind, 8);
Uint = NewBasicType("uint", UintKind, 4); // TODO: need to know how big a uint is
Uint8 = NewBasicType("uint8", Uint8Kind, 1); Uint8 = NewBasicType("uint8", Uint8Kind, 1);
Uint16 = NewBasicType("uint16", Uint16Kind, 2); Uint16 = NewBasicType("uint16", Uint16Kind, 2);
Uint32 = NewBasicType("uint32", Uint32Kind, 4); Uint32 = NewBasicType("uint32", Uint32Kind, 4);
Uint64 = NewBasicType("uint64", Uint64Kind, 8); Uint64 = NewBasicType("uint64", Uint64Kind, 8);
Float = NewBasicType("float", FloatKind, 4); // TODO: need to know how big a float is
Float32 = NewBasicType("float32", Float32Kind, 4); Float32 = NewBasicType("float32", Float32Kind, 4);
Float64 = NewBasicType("float64", Float64Kind, 8); Float64 = NewBasicType("float64", Float64Kind, 8);
Float80 = NewBasicType("float80", Float80Kind, 10); // TODO: strange size? Float80 = NewBasicType("float80", Float80Kind, 10); // TODO: strange size?
...@@ -387,14 +393,17 @@ func init() { ...@@ -387,14 +393,17 @@ func init() {
// Basics go into types table // Basics go into types table
types[MissingString] = &Missing; types[MissingString] = &Missing;
types["int"] = &Int;
types["int8"] = &Int8; types["int8"] = &Int8;
types["int16"] = &Int16; types["int16"] = &Int16;
types["int32"] = &Int32; types["int32"] = &Int32;
types["int64"] = &Int64; types["int64"] = &Int64;
types["uint"] = &Uint;
types["uint8"] = &Uint8; types["uint8"] = &Uint8;
types["uint16"] = &Uint16; types["uint16"] = &Uint16;
types["uint32"] = &Uint32; types["uint32"] = &Uint32;
types["uint64"] = &Uint64; types["uint64"] = &Uint64;
types["float"] = &Float;
types["float32"] = &Float32; types["float32"] = &Float32;
types["float64"] = &Float64; types["float64"] = &Float64;
types["float80"] = &Float80; types["float80"] = &Float80;
...@@ -403,14 +412,17 @@ func init() { ...@@ -403,14 +412,17 @@ func init() {
// Basics get prebuilt stubs // Basics get prebuilt stubs
MissingStub = NewStubType(MissingString, Missing); MissingStub = NewStubType(MissingString, Missing);
basicstub[MissingString] = MissingStub; basicstub[MissingString] = MissingStub;
basicstub["int"] = NewStubType("int", Int);
basicstub["int8"] = NewStubType("int8", Int8); basicstub["int8"] = NewStubType("int8", Int8);
basicstub["int16"] = NewStubType("int16", Int16); basicstub["int16"] = NewStubType("int16", Int16);
basicstub["int32"] = NewStubType("int32", Int32); basicstub["int32"] = NewStubType("int32", Int32);
basicstub["int64"] = NewStubType("int64", Int64); basicstub["int64"] = NewStubType("int64", Int64);
basicstub["uint"] = NewStubType("uint", Uint);
basicstub["uint8"] = NewStubType("uint8", Uint8); basicstub["uint8"] = NewStubType("uint8", Uint8);
basicstub["uint16"] = NewStubType("uint16", Uint16); basicstub["uint16"] = NewStubType("uint16", Uint16);
basicstub["uint32"] = NewStubType("uint32", Uint32); basicstub["uint32"] = NewStubType("uint32", Uint32);
basicstub["uint64"] = NewStubType("uint64", Uint64); basicstub["uint64"] = NewStubType("uint64", Uint64);
basicstub["float"] = NewStubType("float", Float);
basicstub["float32"] = NewStubType("float32", Float32); basicstub["float32"] = NewStubType("float32", Float32);
basicstub["float64"] = NewStubType("float64", Float64); basicstub["float64"] = NewStubType("float64", Float64);
basicstub["float80"] = NewStubType("float80", Float80); basicstub["float80"] = NewStubType("float80", Float80);
......
...@@ -41,21 +41,49 @@ type Creator *(typ Type, addr Addr) Value ...@@ -41,21 +41,49 @@ type Creator *(typ Type, addr Addr) Value
// Conversion functions, implemented in assembler // Conversion functions, implemented in assembler
func AddrToPtrAddr(Addr) *Addr func AddrToPtrAddr(Addr) *Addr
func AddrToPtrInt(Addr) *int
func AddrToPtrInt8(Addr) *int8 func AddrToPtrInt8(Addr) *int8
func AddrToPtrInt16(Addr) *int16 func AddrToPtrInt16(Addr) *int16
func AddrToPtrInt32(Addr) *int32 func AddrToPtrInt32(Addr) *int32
func AddrToPtrInt64(Addr) *int64 func AddrToPtrInt64(Addr) *int64
func AddrToPtrUint(Addr) *uint
func AddrToPtrUint8(Addr) *uint8 func AddrToPtrUint8(Addr) *uint8
func PtrUint8ToAddr(*uint8) Addr func PtrUint8ToAddr(*uint8) Addr
func AddrToPtrUint16(Addr) *uint16 func AddrToPtrUint16(Addr) *uint16
func AddrToPtrUint32(Addr) *uint32 func AddrToPtrUint32(Addr) *uint32
func AddrToPtrUint64(Addr) *uint64 func AddrToPtrUint64(Addr) *uint64
func PtrUint64ToAddr(*uint64) Addr func PtrUint64ToAddr(*uint64) Addr
func AddrToPtrFloat(Addr) *float
func AddrToPtrFloat32(Addr) *float32 func AddrToPtrFloat32(Addr) *float32
func AddrToPtrFloat64(Addr) *float64 func AddrToPtrFloat64(Addr) *float64
func AddrToPtrFloat80(Addr) *float80 func AddrToPtrFloat80(Addr) *float80
func AddrToPtrString(Addr) *string func AddrToPtrString(Addr) *string
// -- Int
export type IntValue interface {
Kind() int;
Get() int;
Put(int);
Type() Type;
}
type IntValueStruct struct {
CommonV
}
func IntCreator(typ Type, addr Addr) Value {
return &IntValueStruct{ CommonV{IntKind, typ, addr} }
}
func (v *IntValueStruct) Get() int {
return *AddrToPtrInt(v.addr)
}
func (v *IntValueStruct) Put(i int) {
*AddrToPtrInt(v.addr) = i
}
// -- Int8 // -- Int8
export type Int8Value interface { export type Int8Value interface {
...@@ -156,6 +184,31 @@ func (v *Int64ValueStruct) Put(i int64) { ...@@ -156,6 +184,31 @@ func (v *Int64ValueStruct) Put(i int64) {
*AddrToPtrInt64(v.addr) = i *AddrToPtrInt64(v.addr) = i
} }
// -- Uint
export type UintValue interface {
Kind() int;
Get() uint;
Put(uint);
Type() Type;
}
type UintValueStruct struct {
CommonV
}
func UintCreator(typ Type, addr Addr) Value {
return &UintValueStruct{ CommonV{UintKind, typ, addr} }
}
func (v *UintValueStruct) Get() uint {
return *AddrToPtrUint(v.addr)
}
func (v *UintValueStruct) Put(i uint) {
*AddrToPtrUint(v.addr) = i
}
// -- Uint8 // -- Uint8
export type Uint8Value interface { export type Uint8Value interface {
...@@ -256,6 +309,31 @@ func (v *Uint64ValueStruct) Put(i uint64) { ...@@ -256,6 +309,31 @@ func (v *Uint64ValueStruct) Put(i uint64) {
*AddrToPtrUint64(v.addr) = i *AddrToPtrUint64(v.addr) = i
} }
// -- Float
export type FloatValue interface {
Kind() int;
Get() float;
Put(float);
Type() Type;
}
type FloatValueStruct struct {
CommonV
}
func FloatCreator(typ Type, addr Addr) Value {
return &FloatValueStruct{ CommonV{FloatKind, typ, addr} }
}
func (v *FloatValueStruct) Get() float {
return *AddrToPtrFloat(v.addr)
}
func (v *FloatValueStruct) Put(f float) {
*AddrToPtrFloat(v.addr) = f
}
// -- Float32 // -- Float32
export type Float32Value interface { export type Float32Value interface {
...@@ -572,14 +650,17 @@ var creator *map[int] Creator ...@@ -572,14 +650,17 @@ var creator *map[int] Creator
func init() { func init() {
creator = new(map[int] Creator); creator = new(map[int] Creator);
creator[IntKind] = &IntCreator;
creator[Int8Kind] = &Int8Creator; creator[Int8Kind] = &Int8Creator;
creator[Int16Kind] = &Int16Creator; creator[Int16Kind] = &Int16Creator;
creator[Int32Kind] = &Int32Creator; creator[Int32Kind] = &Int32Creator;
creator[Int64Kind] = &Int64Creator; creator[Int64Kind] = &Int64Creator;
creator[UintKind] = &UintCreator;
creator[Uint8Kind] = &Uint8Creator; creator[Uint8Kind] = &Uint8Creator;
creator[Uint16Kind] = &Uint16Creator; creator[Uint16Kind] = &Uint16Creator;
creator[Uint32Kind] = &Uint32Creator; creator[Uint32Kind] = &Uint32Creator;
creator[Uint64Kind] = &Uint64Creator; creator[Uint64Kind] = &Uint64Creator;
creator[FloatKind] = &FloatCreator;
creator[Float32Kind] = &Float32Creator; creator[Float32Kind] = &Float32Creator;
creator[Float64Kind] = &Float64Creator; creator[Float64Kind] = &Float64Creator;
creator[Float80Kind] = &Float80Creator; creator[Float80Kind] = &Float80Creator;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment