/
type.go
556 lines (467 loc) · 12.4 KB
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package gcssa
import (
"go/types"
"github.com/bjwbell/ssa"
)
type Type struct {
n *Node
types.Type
}
func StdSizes() types.StdSizes {
var std types.StdSizes
// TODO: make dependent on arch
std.WordSize = 8
std.MaxAlign = 8
return std
}
var typeToEtype = map[types.BasicKind]int64{
types.Bool: TBOOL,
types.Int: TINT,
types.Int8: TINT8,
types.Int16: TINT16,
types.Int32: TINT32,
types.Int64: TINT64,
types.Uint: TUINT,
types.Uint8: TUINT8,
types.Uint16: TUINT16,
types.Uint32: TUINT32,
types.Uint64: TUINT64,
types.Uintptr: TUINTPTR,
types.Float32: TFLOAT32,
types.Float64: TFLOAT64,
types.Complex64: TCOMPLEX64,
types.Complex128: TCOMPLEX128,
types.String: TSTRING,
types.UnsafePointer: TUNSAFEPTR,
// types for untyped values
/*types.UntypedBool: CTBOOL,
types.UntypedInt: CTINT,
types.UntypedRune: CTRUNE,
types.UntypedFloat: CTFLT,
types.UntypedComplex: CTCPLX,
types.UntypedString: CTSTR,
types.UntypedNil: CTNIL*/}
// Basic returns *types.Basic if t.Type is *types.Basic
// else nil is returned.
func (t *Type) Basic() *types.Basic {
if basic, ok := t.Type.(*types.Basic); ok {
return basic
}
return nil
}
// Struct returns *types.Struct if t.Type is *types.Struct
// else nil is returned.
func (t *Type) Struct() *types.Struct {
if s, ok := t.Type.(*types.Struct); ok {
return s
}
return nil
}
// Array returns *types.Array if t.Type is *types.Array
// else nil is returned.
func (t *Type) Array() *types.Array {
if array, ok := t.Type.(*types.Array); ok {
return array
}
return nil
}
// IsBasicInfoFlag returns true if t.Type is types.Basic and
// the BasicInfo for t.Type matches flags, otherwise false is returned
func (t *Type) IsBasicInfoFlag(flag types.BasicInfo) bool {
if basic := t.Basic(); basic != nil {
info := basic.Info()
return info&flag == 1
} else {
return false
}
}
func (t *Type) IsBasic() bool {
return t.Basic() != nil
}
func (t *Type) IsBoolean() bool {
return t.IsBasicInfoFlag(types.IsBoolean)
}
func (t *Type) IsInteger() bool {
return t.IsBasicInfoFlag(types.IsInteger)
}
func (t *Type) IsSigned() bool {
return (t.IsBasic() && !t.IsBasicInfoFlag(types.IsUnsigned))
}
func (t *Type) IsFloat() bool {
return t.IsBasicInfoFlag(types.IsFloat)
}
func (t *Type) IsComplex() bool {
return t.IsBasicInfoFlag(types.IsComplex)
}
func (t *Type) IsPtr() bool {
// behavior should match:
// Etype == TPTR32 || Etype == TPTR64 || Etype == TUNSAFEPTR ||
// Etype == TMAP || Etype == TCHAN || Etype == TFUNC
if basic := t.Basic(); basic != nil {
return basic.Kind() == types.UnsafePointer
}
switch t.Type.(type) {
case *types.Pointer, *types.Map, *types.Signature, *types.Chan:
return true
}
return false
}
func (t *Type) IsString() bool {
return t.IsBasicInfoFlag(types.IsString)
}
func (t *Type) IsMap() bool {
_, ok := t.Type.(*types.Map)
return ok
}
func (t *Type) IsChan() bool {
_, ok := t.Type.(*types.Map)
return ok
}
func (t *Type) IsSlice() bool {
_, ok := t.Type.(*types.Slice)
return ok
}
func (t *Type) IsArray() bool {
_, ok := t.Type.(*types.Array)
return ok
}
func (t *Type) IsStruct() bool {
return t.Struct() != nil
}
func (t *Type) IsInterface() bool {
_, ok := t.Type.(*types.Interface)
return ok
}
func (t *Type) Size() int64 {
std := StdSizes()
return std.Sizeof(t.Type)
}
func (t *Type) Alignment() int64 {
std := StdSizes()
return std.Alignof(t.Type)
}
func (t *Type) IsMemory() bool { return false } // special ssa-package-only types
func (t *Type) IsFlags() bool { return false }
func (t *Type) IsVoid() bool { return false }
// Elem, if t.Type is []T or *T or [n]T, return T, otherwise return nil
func (t *Type) Elem() ssa.Type {
if t.IsSlice() || t.IsPtr() || t.IsArray() {
return &Type{t.n, t.Underlying()}
} else {
return nil
}
}
// PtrTo, given T, returns *T
func (t *Type) PtrTo() ssa.Type {
return &Type{t.n, types.NewPointer(t.Type)}
}
// NumFields returns the # of fields of a struct, panics if t is not a types.Struct
func (t *Type) NumFields() int64 {
if !t.IsStruct() {
panic("NumFields can only be called with Struct's")
}
s := t.Type.(*types.Struct)
return int64(s.NumFields())
}
// FieldTypes returns the type of ith field of the struct and panics on error
func (t *Type) FieldType(i int64) ssa.Type {
if s := t.Struct(); s == nil {
panic("FieldType can only be called with Struct's")
} else {
if int64(s.NumFields()) <= i {
panic("Invalid field #")
}
// TODO: figure out what node i.e. t.n to use
field := Type{nil, s.Field(int(i)).Type()}
return &field
}
}
// FieldOff returns the offset of ith field of the struct and panics on error
func (t *Type) FieldOff(i int64) int64 {
if s := t.Struct(); s == nil {
panic("FieldOff can only be called with Struct's")
} else {
if int64(s.NumFields()) <= i {
panic("Invalid field #")
}
std := StdSizes()
field := s.Field(int(i))
offsets := std.Offsetsof([]*types.Var{field})
return offsets[0]
}
}
// NumElem returns the # of elements of an array and panics on error
func (t *Type) NumElem() int64 {
if array := t.Array(); array == nil {
panic("NumElem can only be called with types.Array")
} else {
return array.Len()
}
}
func (t *Type) String() string {
return t.Type.String()
}
// SimpleString is a coarser generic description of T, e.g. T's underlying type
func (t *Type) SimpleString() string {
return t.Type.Underlying().String()
}
func (t *Type) Equal(v ssa.Type) bool {
if v2, ok := v.(*Type); ok {
return types.Identical(t, v2)
}
return false
}
// Bound returns the num elements if t is an array, if t is a slice it returns -1,
// and if t is neither an array or slice it panics
func (t *Type) Bound() int64 {
if t.Array() != nil {
return t.NumElem()
} else if t.IsSlice() {
return -1
} else {
panic("Bound called with invalid type")
}
}
func (t *Type) Width() int64 {
return t.Size()
}
// Etype is the concrete type, e.g. TBOOL, TINT64, TFIELD, etc
func (t *Type) Etype() int64 {
if basic := t.Basic(); basic != nil {
if etype, ok := typeToEtype[basic.Kind()]; ok {
return etype
} else {
panic("unknown basic type")
}
}
switch t.Type.(type) {
case *types.Array:
return TARRAY
case *types.Chan:
return TCHAN
case *types.Interface:
return TINTER
case *types.Map:
return TMAP
case *types.Named:
panic("unimplemented")
case *types.Pointer:
// HACK!
// hardcoded to 64-bit pointers
return TPTR64
case *types.Signature:
return TFUNC
case *types.Slice:
panic("unimplemented")
case *types.Struct:
return TSTRUCT
case *types.Tuple:
panic("unimplemented")
}
// TODO figure out what should be done for types.Var
//case *types.Var:
// return TFIELD
panic("unimplemented")
}
func typ(etype int) *Type {
for t, et := range typeToEtype {
if et == int64(etype) {
return &Type{nil, types.Typ[t]}
}
}
return nil
}
func typeinit() {
if Widthptr == 0 {
Fatalf("typeinit before betypeinit")
}
for i := 0; i < NTYPE; i++ {
Simtype[i] = uint8(i)
}
Tptr = TPTR32
if Widthptr == 8 {
Tptr = TPTR64
}
for i := TINT8; i <= TUINT64; i++ {
Isint[i] = true
}
Types[TINT8] = typ(TINT8)
Types[TUINT8] = typ(TUINT8)
Types[TINT16] = typ(TINT16)
Types[TUINT16] = typ(TUINT16)
Types[TINT32] = typ(TINT32)
Types[TUINT32] = typ(TUINT32)
Types[TINT64] = typ(TINT64)
Types[TUINT64] = typ(TUINT64)
Types[TINT] = typ(TINT)
Types[TUINT] = typ(TUINT)
Types[TUINTPTR] = typ(TUINTPTR)
Types[TCOMPLEX64] = typ(TCOMPLEX64)
Types[TCOMPLEX128] = typ(TCOMPLEX128)
Types[TFLOAT32] = typ(TFLOAT32)
Types[TFLOAT64] = typ(TFLOAT64)
Types[TBOOL] = typ(TBOOL)
Types[TPTR32] = typ(TPTR32)
Types[TPTR64] = typ(TPTR64)
Types[TFUNC] = typ(TFUNC)
Types[TARRAY] = typ(TARRAY)
Types[TSTRUCT] = typ(TSTRUCT)
Types[TCHAN] = typ(TCHAN)
Types[TMAP] = typ(TMAP)
Types[TINTER] = typ(TINTER)
Types[TFIELD] = typ(TFIELD)
Types[TANY] = typ(TANY)
Types[TSTRING] = typ(TSTRING)
Types[TUNSAFEPTR] = typ(TUNSAFEPTR)
// pseudo-types for literals
Types[TIDEAL] = typ(TIDEAL)
Types[TNIL] = typ(TNIL)
Types[TBLANK] = typ(TBLANK)
// pseudo-type for frame layout
Types[TFUNCARGS] = typ(TFUNCARGS)
Types[TCHANARGS] = typ(TCHANARGS)
Types[TINTERMETH] = typ(TINTERMETH)
Isint[TINT] = true
Isint[TUINT] = true
Isint[TUINTPTR] = true
Isfloat[TFLOAT32] = true
Isfloat[TFLOAT64] = true
Iscomplex[TCOMPLEX64] = true
Iscomplex[TCOMPLEX128] = true
Isptr[TPTR32] = true
Isptr[TPTR64] = true
Issigned[TINT] = true
Issigned[TINT8] = true
Issigned[TINT16] = true
Issigned[TINT32] = true
Issigned[TINT64] = true
/*
* initialize okfor
*/
for i := 0; i < NTYPE; i++ {
if Isint[i] || i == TIDEAL {
okforeq[i] = true
okforcmp[i] = true
okforarith[i] = true
okforadd[i] = true
okforand[i] = true
okforconst[i] = true
issimple[i] = true
Minintval[i] = new(Mpint)
Maxintval[i] = new(Mpint)
}
if Isfloat[i] {
okforeq[i] = true
okforcmp[i] = true
okforadd[i] = true
okforarith[i] = true
okforconst[i] = true
issimple[i] = true
//minfltval[i] = newMpflt()
//maxfltval[i] = newMpflt()
}
if Iscomplex[i] {
okforeq[i] = true
okforadd[i] = true
okforarith[i] = true
okforconst[i] = true
issimple[i] = true
}
}
issimple[TBOOL] = true
okforadd[TSTRING] = true
okforbool[TBOOL] = true
okforcap[TARRAY] = true
okforcap[TCHAN] = true
okforconst[TBOOL] = true
okforconst[TSTRING] = true
okforlen[TARRAY] = true
okforlen[TCHAN] = true
okforlen[TMAP] = true
okforlen[TSTRING] = true
okforeq[TPTR32] = true
okforeq[TPTR64] = true
okforeq[TUNSAFEPTR] = true
okforeq[TINTER] = true
okforeq[TCHAN] = true
okforeq[TSTRING] = true
okforeq[TBOOL] = true
okforeq[TMAP] = true // nil only; refined in typecheck
okforeq[TFUNC] = true // nil only; refined in typecheck
okforeq[TARRAY] = true // nil slice only; refined in typecheck
okforeq[TSTRUCT] = true // it's complicated; refined in typecheck
okforcmp[TSTRING] = true
var i int
for i = 0; i < len(okfor); i++ {
okfor[i] = okfornone[:]
}
// binary
okfor[OADD] = okforadd[:]
okfor[OAND] = okforand[:]
okfor[OANDAND] = okforbool[:]
okfor[OANDNOT] = okforand[:]
okfor[ODIV] = okforarith[:]
okfor[OEQ] = okforeq[:]
okfor[OGE] = okforcmp[:]
okfor[OGT] = okforcmp[:]
okfor[OLE] = okforcmp[:]
okfor[OLT] = okforcmp[:]
okfor[OMOD] = okforand[:]
okfor[OHMUL] = okforarith[:]
okfor[OMUL] = okforarith[:]
okfor[ONE] = okforeq[:]
okfor[OOR] = okforand[:]
okfor[OOROR] = okforbool[:]
okfor[OSUB] = okforarith[:]
okfor[OXOR] = okforand[:]
okfor[OLSH] = okforand[:]
okfor[ORSH] = okforand[:]
// unary
okfor[OCOM] = okforand[:]
okfor[OMINUS] = okforarith[:]
okfor[ONOT] = okforbool[:]
okfor[OPLUS] = okforarith[:]
// special
okfor[OCAP] = okforcap[:]
okfor[OLEN] = okforlen[:]
// comparison
iscmp[OLT] = true
iscmp[OGT] = true
iscmp[OGE] = true
iscmp[OLE] = true
iscmp[OEQ] = true
iscmp[ONE] = true
/*mpatofix(Maxintval[TINT8], "0x7f")
mpatofix(Minintval[TINT8], "-0x80")
mpatofix(Maxintval[TINT16], "0x7fff")
mpatofix(Minintval[TINT16], "-0x8000")
mpatofix(Maxintval[TINT32], "0x7fffffff")
mpatofix(Minintval[TINT32], "-0x80000000")
mpatofix(Maxintval[TINT64], "0x7fffffffffffffff")
mpatofix(Minintval[TINT64], "-0x8000000000000000")
mpatofix(Maxintval[TUINT8], "0xff")
mpatofix(Maxintval[TUINT16], "0xffff")
mpatofix(Maxintval[TUINT32], "0xffffffff")
mpatofix(Maxintval[TUINT64], "0xffffffffffffffff")*/
/* f is valid float if min < f < max. (min and max are not themselves valid.) */
//mpatoflt(maxfltval[TFLOAT32], "33554431p103") /* 2^24-1 p (127-23) + 1/2 ulp*/
//mpatoflt(minfltval[TFLOAT32], "-33554431p103")
//mpatoflt(maxfltval[TFLOAT64], "18014398509481983p970") /* 2^53-1 p (1023-52) + 1/2 ulp */
//mpatoflt(minfltval[TFLOAT64], "-18014398509481983p970")
maxfltval[TCOMPLEX64] = maxfltval[TFLOAT32]
minfltval[TCOMPLEX64] = minfltval[TFLOAT32]
maxfltval[TCOMPLEX128] = maxfltval[TFLOAT64]
minfltval[TCOMPLEX128] = minfltval[TFLOAT64]
/* simple aliases */
Simtype[TMAP] = uint8(Tptr)
Simtype[TCHAN] = uint8(Tptr)
Simtype[TFUNC] = uint8(Tptr)
Simtype[TUNSAFEPTR] = uint8(Tptr)
/* pick up the backend thearch.typedefs */
Array_array = int(Rnd(0, int64(Widthptr)))
Array_nel = int(Rnd(int64(Array_array)+int64(Widthptr), int64(Widthint)))
Array_cap = int(Rnd(int64(Array_nel)+int64(Widthint), int64(Widthint)))
sizeof_Array = int(Rnd(int64(Array_cap)+int64(Widthint), int64(Widthptr)))
// string is same as slice wo the cap
sizeof_String = int(Rnd(int64(Array_nel)+int64(Widthint), int64(Widthptr)))
}