1
2
3
4
5
6
7
8
9
10
11
12 package rttype
13
14 import (
15 "cmd/compile/internal/base"
16 "cmd/compile/internal/objw"
17 "cmd/compile/internal/types"
18 "cmd/internal/obj"
19 "internal/abi"
20 "reflect"
21 )
22
23
24 var Type *types.Type
25
26 var ArrayType *types.Type
27 var ChanType *types.Type
28 var FuncType *types.Type
29 var InterfaceType *types.Type
30 var MapType *types.Type
31 var PtrType *types.Type
32 var SliceType *types.Type
33 var StructType *types.Type
34
35
36 var IMethod *types.Type
37 var Method *types.Type
38 var StructField *types.Type
39 var UncommonType *types.Type
40
41
42 var InterfaceSwitch *types.Type
43 var TypeAssert *types.Type
44
45
46 var ITab *types.Type
47
48 func Init() {
49
50
51
52 Type = FromReflect(reflect.TypeFor[abi.Type]())
53 ArrayType = FromReflect(reflect.TypeFor[abi.ArrayType]())
54 ChanType = FromReflect(reflect.TypeFor[abi.ChanType]())
55 FuncType = FromReflect(reflect.TypeFor[abi.FuncType]())
56 InterfaceType = FromReflect(reflect.TypeFor[abi.InterfaceType]())
57 MapType = FromReflect(reflect.TypeFor[abi.MapType]())
58 PtrType = FromReflect(reflect.TypeFor[abi.PtrType]())
59 SliceType = FromReflect(reflect.TypeFor[abi.SliceType]())
60 StructType = FromReflect(reflect.TypeFor[abi.StructType]())
61
62 IMethod = FromReflect(reflect.TypeFor[abi.Imethod]())
63 Method = FromReflect(reflect.TypeFor[abi.Method]())
64 StructField = FromReflect(reflect.TypeFor[abi.StructField]())
65 UncommonType = FromReflect(reflect.TypeFor[abi.UncommonType]())
66
67 InterfaceSwitch = FromReflect(reflect.TypeFor[abi.InterfaceSwitch]())
68 TypeAssert = FromReflect(reflect.TypeFor[abi.TypeAssert]())
69
70 ITab = FromReflect(reflect.TypeFor[abi.ITab]())
71
72
73
74
75 ptrSize := types.PtrSize
76 if got, want := int64(abi.CommonSize(ptrSize)), Type.Size(); got != want {
77 base.Fatalf("abi.CommonSize() == %d, want %d", got, want)
78 }
79 if got, want := int64(abi.StructFieldSize(ptrSize)), StructField.Size(); got != want {
80 base.Fatalf("abi.StructFieldSize() == %d, want %d", got, want)
81 }
82 if got, want := int64(abi.UncommonSize()), UncommonType.Size(); got != want {
83 base.Fatalf("abi.UncommonSize() == %d, want %d", got, want)
84 }
85 if got, want := int64(abi.TFlagOff(ptrSize)), Type.OffsetOf("TFlag"); got != want {
86 base.Fatalf("abi.TFlagOff() == %d, want %d", got, want)
87 }
88 if got, want := int64(abi.ITabTypeOff(ptrSize)), ITab.OffsetOf("Type"); got != want {
89 base.Fatalf("abi.ITabTypeOff() == %d, want %d", got, want)
90 }
91 for _, test := range []struct {
92 kind abi.Kind
93 typ *types.Type
94 name string
95 }{
96 {abi.Struct, StructType, "Struct"},
97 {abi.Pointer, PtrType, "Pointer"},
98 {abi.Func, FuncType, "Func"},
99 {abi.Slice, SliceType, "Slice"},
100 {abi.Array, ArrayType, "Array"},
101 {abi.Chan, ChanType, "Chan"},
102 {abi.Map, MapType, "Map"},
103 {abi.Interface, InterfaceType, "Interface"},
104 } {
105 if got, want := int64(abi.RTypeSize(test.kind, ptrSize)), test.typ.Size(); got != want {
106 base.Fatalf("abi.RTypeSize(%s) == %d, want %d", test.name, got, want)
107 }
108 }
109 }
110
111
112 func FromReflect(rt reflect.Type) *types.Type {
113 t := reflectToType(rt)
114 types.CalcSize(t)
115 return t
116 }
117
118
119
120
121 func reflectToType(rt reflect.Type) *types.Type {
122 switch rt.Kind() {
123 case reflect.Bool:
124 return types.Types[types.TBOOL]
125 case reflect.Int:
126 return types.Types[types.TINT]
127 case reflect.Int8:
128 return types.Types[types.TINT8]
129 case reflect.Int16:
130 return types.Types[types.TINT16]
131 case reflect.Int32:
132 return types.Types[types.TINT32]
133 case reflect.Uint8:
134 return types.Types[types.TUINT8]
135 case reflect.Uint16:
136 return types.Types[types.TUINT16]
137 case reflect.Uint32:
138 return types.Types[types.TUINT32]
139 case reflect.Float32:
140 return types.Types[types.TFLOAT32]
141 case reflect.Float64:
142 return types.Types[types.TFLOAT64]
143 case reflect.Uintptr:
144 return types.Types[types.TUINTPTR]
145 case reflect.Ptr:
146 return types.NewPtr(reflectToType(rt.Elem()))
147 case reflect.Func, reflect.UnsafePointer:
148
149
150 return types.Types[types.TUNSAFEPTR]
151 case reflect.Slice:
152 return types.NewSlice(reflectToType(rt.Elem()))
153 case reflect.Array:
154 return types.NewArray(reflectToType(rt.Elem()), int64(rt.Len()))
155 case reflect.Struct:
156 fields := make([]*types.Field, rt.NumField())
157 for i := 0; i < rt.NumField(); i++ {
158 f := rt.Field(i)
159 ft := reflectToType(f.Type)
160 fields[i] = &types.Field{Sym: &types.Sym{Name: f.Name}, Type: ft}
161 }
162 return types.NewStruct(fields)
163 case reflect.Chan:
164 return types.NewChan(reflectToType(rt.Elem()), types.ChanDir(rt.ChanDir()))
165 case reflect.String:
166 return types.Types[types.TSTRING]
167 case reflect.Complex128:
168 return types.Types[types.TCOMPLEX128]
169 default:
170 base.Fatalf("unhandled kind %s", rt.Kind())
171 return nil
172 }
173 }
174
175
176
177 type Cursor struct {
178 lsym *obj.LSym
179 offset int64
180 typ *types.Type
181 }
182
183
184 func NewCursor(lsym *obj.LSym, off int64, t *types.Type) Cursor {
185 return Cursor{lsym: lsym, offset: off, typ: t}
186 }
187
188
189 func (c Cursor) WritePtr(target *obj.LSym) {
190 if c.typ.Kind() != types.TUNSAFEPTR && c.typ.Kind() != types.TPTR {
191 base.Fatalf("can't write ptr, it has kind %s", c.typ.Kind())
192 }
193 if target == nil {
194 objw.Uintptr(c.lsym, int(c.offset), 0)
195 } else {
196 objw.SymPtr(c.lsym, int(c.offset), target, 0)
197 }
198 }
199 func (c Cursor) WritePtrWeak(target *obj.LSym) {
200 if c.typ.Kind() != types.TUINTPTR {
201 base.Fatalf("can't write ptr, it has kind %s", c.typ.Kind())
202 }
203 objw.SymPtrWeak(c.lsym, int(c.offset), target, 0)
204 }
205
206
207 func (c Cursor) WriteFuncPtrWeak(target *obj.LSym) {
208 if c.typ.Kind() != types.TUINTPTR {
209 base.Fatalf("can't write weak func ptr, it has kind %s", c.typ.Kind())
210 }
211 objw.SymFuncPtrWeak(c.lsym, int(c.offset), target)
212 }
213 func (c Cursor) WriteUintptr(val uint64) {
214 if c.typ.Kind() != types.TUINTPTR {
215 base.Fatalf("can't write uintptr, it has kind %s", c.typ.Kind())
216 }
217 objw.Uintptr(c.lsym, int(c.offset), val)
218 }
219 func (c Cursor) WriteUint32(val uint32) {
220 if c.typ.Kind() != types.TUINT32 {
221 base.Fatalf("can't write uint32, it has kind %s", c.typ.Kind())
222 }
223 objw.Uint32(c.lsym, int(c.offset), val)
224 }
225 func (c Cursor) WriteUint16(val uint16) {
226 if c.typ.Kind() != types.TUINT16 {
227 base.Fatalf("can't write uint16, it has kind %s", c.typ.Kind())
228 }
229 objw.Uint16(c.lsym, int(c.offset), val)
230 }
231 func (c Cursor) WriteUint8(val uint8) {
232 if c.typ.Kind() != types.TUINT8 {
233 base.Fatalf("can't write uint8, it has kind %s", c.typ.Kind())
234 }
235 objw.Uint8(c.lsym, int(c.offset), val)
236 }
237 func (c Cursor) WriteInt(val int64) {
238 if c.typ.Kind() != types.TINT {
239 base.Fatalf("can't write int, it has kind %s", c.typ.Kind())
240 }
241 objw.Uintptr(c.lsym, int(c.offset), uint64(val))
242 }
243 func (c Cursor) WriteInt32(val int32) {
244 if c.typ.Kind() != types.TINT32 {
245 base.Fatalf("can't write int32, it has kind %s", c.typ.Kind())
246 }
247 objw.Uint32(c.lsym, int(c.offset), uint32(val))
248 }
249 func (c Cursor) WriteBool(val bool) {
250 if c.typ.Kind() != types.TBOOL {
251 base.Fatalf("can't write bool, it has kind %s", c.typ.Kind())
252 }
253 objw.Bool(c.lsym, int(c.offset), val)
254 }
255
256
257
258 func (c Cursor) WriteSymPtrOff(target *obj.LSym, weak bool) {
259 if c.typ.Kind() != types.TINT32 && c.typ.Kind() != types.TUINT32 {
260 base.Fatalf("can't write SymPtr, it has kind %s", c.typ.Kind())
261 }
262 if target == nil {
263 objw.Uint32(c.lsym, int(c.offset), 0)
264 } else if weak {
265 objw.SymPtrWeakOff(c.lsym, int(c.offset), target)
266 } else {
267 objw.SymPtrOff(c.lsym, int(c.offset), target)
268 }
269 }
270
271
272 func (c Cursor) WriteSlice(target *obj.LSym, off, len, cap int64) {
273 if c.typ.Kind() != types.TSLICE {
274 base.Fatalf("can't write slice, it has kind %s", c.typ.Kind())
275 }
276 objw.SymPtr(c.lsym, int(c.offset), target, int(off))
277 objw.Uintptr(c.lsym, int(c.offset)+types.PtrSize, uint64(len))
278 objw.Uintptr(c.lsym, int(c.offset)+2*types.PtrSize, uint64(cap))
279
280
281 if len != cap {
282 base.Fatalf("len != cap (%d != %d)", len, cap)
283 }
284 }
285
286
287
288 func (c Cursor) Reloc(rel obj.Reloc) {
289 rel.Off = int32(c.offset)
290 rel.Siz = uint8(c.typ.Size())
291 c.lsym.AddRel(base.Ctxt, rel)
292 }
293
294
295 func (c Cursor) Field(name string) Cursor {
296 if c.typ.Kind() != types.TSTRUCT {
297 base.Fatalf("can't call Field on non-struct %v", c.typ)
298 }
299 for _, f := range c.typ.Fields() {
300 if f.Sym.Name == name {
301 return Cursor{lsym: c.lsym, offset: c.offset + f.Offset, typ: f.Type}
302 }
303 }
304 base.Fatalf("couldn't find field %s in %v", name, c.typ)
305 return Cursor{}
306 }
307
308 func (c Cursor) Elem(i int64) Cursor {
309 if c.typ.Kind() != types.TARRAY {
310 base.Fatalf("can't call Elem on non-array %v", c.typ)
311 }
312 if i < 0 || i >= c.typ.NumElem() {
313 base.Fatalf("element access out of bounds [%d] in [0:%d]", i, c.typ.NumElem())
314 }
315 elem := c.typ.Elem()
316 return Cursor{lsym: c.lsym, offset: c.offset + i*elem.Size(), typ: elem}
317 }
318
319 type ArrayCursor struct {
320 c Cursor
321 n int
322 }
323
324
325 func NewArrayCursor(lsym *obj.LSym, off int64, t *types.Type, n int) ArrayCursor {
326 return ArrayCursor{
327 c: NewCursor(lsym, off, t),
328 n: n,
329 }
330 }
331
332
333 func (a ArrayCursor) Elem(i int) Cursor {
334 if i < 0 || i >= a.n {
335 base.Fatalf("element index %d out of range [0:%d]", i, a.n)
336 }
337 return Cursor{lsym: a.c.lsym, offset: a.c.offset + int64(i)*a.c.typ.Size(), typ: a.c.typ}
338 }
339
340
341
342
343 func (c Cursor) ModifyArray(n int) (ArrayCursor, int64) {
344 if c.typ.Kind() != types.TARRAY {
345 base.Fatalf("can't call ModifyArray on non-array %v", c.typ)
346 }
347 k := c.typ.NumElem()
348 return ArrayCursor{c: Cursor{lsym: c.lsym, offset: c.offset, typ: c.typ.Elem()}, n: n}, (int64(n) - k) * c.typ.Elem().Size()
349 }
350
View as plain text