From 1905d7c090a13a8b94e5d19a5388104f2d7693fd Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Sun, 3 May 2020 16:53:36 +0200 Subject: prog: refactor ANY to not fabricate new types Currently ANY implementation fabricates new types dynamically. This is something we don't do anywhere else, generally types come from compiler and all are static. Dynamic types will conflict with use of Ref in Arg optimization. Move ANY types creation into compiler. Update #1580 --- prog/any.go | 99 +++++++++++++------------------------------------------------ 1 file changed, 20 insertions(+), 79 deletions(-) (limited to 'prog/any.go') diff --git a/prog/any.go b/prog/any.go index ce3736a4b..7bf38b6be 100644 --- a/prog/any.go +++ b/prog/any.go @@ -21,88 +21,29 @@ type anyTypes struct { resoct *ResourceType } -// This generates type descriptions for: -// -// resource ANYRES16[int16]: 0xffffffffffffffff, 0 -// resource ANYRES32[int32]: 0xffffffffffffffff, 0 -// resource ANYRES64[int64]: 0xffffffffffffffff, 0 -// ANY [ -// bin array[int8] -// res16 ANYRES16 -// res32 ANYRES32 -// res64 ANYRES64 -// resdec fmt[dec, ANYRES64] -// reshex fmt[hex, ANYRES64] -// resoct fmt[oct, ANYRES64] -// ] [varlen] -func initAnyTypes(target *Target) { - target.any.union = &UnionType{ - TypeCommon: TypeCommon{ - TypeName: "ANYUNION", - IsVarlen: true, - }, - } - target.any.array = &ArrayType{ - TypeCommon: TypeCommon{ - TypeName: "ANYARRAY", - IsVarlen: true, - }, - Elem: target.any.union, - } - target.any.ptrPtr = &PtrType{ - TypeCommon: TypeCommon{ - TypeName: "ANYPTR", - TypeSize: target.PtrSize, - IsOptional: true, - }, - Elem: target.any.array, - ElemDir: DirIn, - } - target.any.ptr64 = &PtrType{ - TypeCommon: TypeCommon{ - TypeName: "ANYPTR64", - TypeSize: 8, - IsOptional: true, - }, - Elem: target.any.array, - ElemDir: DirIn, - } - target.any.blob = &BufferType{ - TypeCommon: TypeCommon{ - TypeName: "ANYBLOB", - IsVarlen: true, - }, - } - createResource := func(name, base string, bf BinaryFormat, size uint64) *ResourceType { - return &ResourceType{ - TypeCommon: TypeCommon{ - TypeName: name, - TypeSize: size, - IsOptional: true, - }, - ArgFormat: bf, - Desc: &ResourceDesc{ - Name: name, - Kind: []string{name}, - Values: []uint64{^uint64(0), 0}, - }, +func (target *Target) initAnyTypes() { + var anyPtrs *UnionType + for _, typ := range target.types { + if typ.Name() == "ANYPTRS" { + anyPtrs = typ.(*UnionType) + break } } - target.any.res16 = createResource("ANYRES16", "int16", FormatNative, 2) - target.any.res32 = createResource("ANYRES32", "int32", FormatNative, 4) - target.any.res64 = createResource("ANYRES64", "int64", FormatNative, 8) - target.any.resdec = createResource("ANYRESDEC", "int64", FormatStrDec, 20) - target.any.reshex = createResource("ANYRESHEX", "int64", FormatStrHex, 18) - target.any.resoct = createResource("ANYRESOCT", "int64", FormatStrOct, 23) - target.any.union.Fields = []Field{ - {Name: "ANYBLOB", Type: target.any.blob}, - {Name: "ANYRES16", Type: target.any.res16}, - {Name: "ANYRES32", Type: target.any.res32}, - {Name: "ANYRES64", Type: target.any.res64}, - {Name: "ANYRESDEC", Type: target.any.resdec}, - {Name: "ANYRESHEX", Type: target.any.reshex}, - {Name: "ANYRESOCT", Type: target.any.resoct}, + if anyPtrs == nil { + panic("no builtin ANYPTRS type") } + // These types are generated by builtin descriptions in pkg/compiler/types.go. + target.any.ptrPtr = anyPtrs.Fields[0].Type.(*PtrType) + target.any.ptr64 = anyPtrs.Fields[1].Type.(*PtrType) + target.any.array = target.any.ptrPtr.Elem.(*ArrayType) + target.any.union = target.any.array.Elem.(*UnionType) + target.any.blob = target.any.union.Fields[0].Type.(*BufferType) + target.any.res16 = target.any.union.Fields[1].Type.(*ResourceType) + target.any.res32 = target.any.union.Fields[2].Type.(*ResourceType) + target.any.res64 = target.any.union.Fields[3].Type.(*ResourceType) + target.any.resdec = target.any.union.Fields[4].Type.(*ResourceType) + target.any.reshex = target.any.union.Fields[5].Type.(*ResourceType) + target.any.resoct = target.any.union.Fields[6].Type.(*ResourceType) } func (target *Target) getAnyPtrType(size uint64) *PtrType { -- cgit mrf-deployment