-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtypes.go
497 lines (431 loc) · 9.53 KB
/
types.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
//
// types.go
//
// Copyright (c) 2020-2024 Markku Rossi
//
// All rights reserved.
//
package types
import (
"fmt"
)
// ID specifies an unique ID for named types.
type ID int32
// Type specifies an MPCL type.
type Type int8
// Size specify sizes and bit counts in circuits.
type Size int32
func (t Type) String() string {
for k, v := range Types {
if v == t {
return k
}
}
return fmt.Sprintf("{Type %d}", t)
}
// ShortString returns a short string name for the type.
func (t Type) ShortString() string {
name, ok := shortTypes[t]
if ok {
return name
}
return t.String()
}
// Array tests if the type is an Array or a Slice.
func (t Type) Array() bool {
return t == TArray || t == TSlice
}
// ByteBits defines the byte size in bits.
const ByteBits = 8
// MPCL types.
const (
TUndefined Type = iota
TBool
TInt
TUint
TFloat
TString
TStruct
TArray
TSlice
TPtr
TNil
)
// Types define MPCL types and their names.
var Types = map[string]Type{
"<Undefined>": TUndefined,
"bool": TBool,
"int": TInt,
"uint": TUint,
"float": TFloat,
"string": TString,
"struct": TStruct,
"array": TArray,
"slice": TSlice,
"ptr": TPtr,
"nil": TNil,
}
var shortTypes = map[Type]string{
TUndefined: "?",
TBool: "b",
TInt: "i",
TUint: "u",
TFloat: "f",
TString: "str",
TStruct: "struct",
TArray: "arr",
TSlice: "slice",
TPtr: "*",
TNil: "nil",
}
// Info specifies information about a type.
type Info struct {
ID ID
Type Type
IsConcrete bool
Bits Size
MinBits Size
Struct []StructField
ElementType *Info
ArraySize Size
Offset Size
}
// Undefined defines type info for undefined types.
var Undefined = Info{
Type: TUndefined,
IsConcrete: true,
}
// Nil defines type info for the nil value.
var Nil = Info{
Type: TNil,
IsConcrete: true,
}
// Bool defines type info for boolean values.
var Bool = Info{
Type: TBool,
IsConcrete: true,
Bits: 1,
MinBits: 1,
}
// Byte defines type info for byte values.
var Byte = Info{
Type: TUint,
IsConcrete: true,
Bits: 8,
MinBits: 8,
}
// Rune defines type info for rune values.
var Rune = Info{
Type: TInt,
IsConcrete: true,
Bits: 32,
MinBits: 32,
}
// Int32 defines type info for signed 32bit integers.
var Int32 = Info{
Type: TInt,
IsConcrete: true,
Bits: 32,
MinBits: 32,
}
// Uint32 defines type info for unsigned 32bit integers.
var Uint32 = Info{
Type: TUint,
IsConcrete: true,
Bits: 32,
MinBits: 32,
}
// Uint64 defines type info for unsigned 64bit integers.
var Uint64 = Info{
Type: TUint,
IsConcrete: true,
Bits: 64,
MinBits: 64,
}
// StructField defines a structure field name and type.
type StructField struct {
Name string
Type Info
}
func (f StructField) String() string {
return fmt.Sprintf("%s[%d:%d]",
f.Type.Type, f.Type.Offset, f.Type.Offset+f.Type.Bits)
}
func (i Info) String() string {
switch i.Type {
case TArray:
return fmt.Sprintf("[%d]%s", i.ArraySize, i.ElementType)
case TSlice:
return fmt.Sprintf("[]%s", i.ElementType)
case TPtr:
return fmt.Sprintf("*%s", i.ElementType)
default:
if !i.Concrete() {
return i.Type.String()
}
return fmt.Sprintf("%s%d", i.Type, i.Bits)
}
}
// ShortString returns a short string name for the type info.
func (i Info) ShortString() string {
if !i.Concrete() {
return i.Type.ShortString()
}
if i.Type == TPtr {
return fmt.Sprintf("*%s", i.ElementType.ShortString())
}
return fmt.Sprintf("%s%d", i.Type.ShortString(), i.Bits)
}
// Undefined tests if type is undefined.
func (i Info) Undefined() bool {
return i.Type == TUndefined
}
// Concrete tests if the type is concrete.
func (i Info) Concrete() bool {
if i.Type != TStruct {
return i.IsConcrete
}
for _, field := range i.Struct {
if !field.Type.Concrete() {
return false
}
}
return true
}
// SetConcrete sets the type concrete status.
func (i *Info) SetConcrete(c bool) {
i.IsConcrete = c
}
// Instantiate instantiates template type to match parameter type.
func (i *Info) Instantiate(o Info) bool {
if i.Type != o.Type {
switch i.Type {
case TArray:
switch o.Type {
case TNil:
// nil instantiates an empty array
if !i.ElementType.Concrete() {
return false
}
i.IsConcrete = true
i.Bits = 0
i.MinBits = 0
i.ArraySize = 0
return true
case TPtr:
if !o.ElementType.Type.Array() {
return false
}
// Instantiating array from pointer to array
// i.e. continue below.
i.Type = TPtr
i.ElementType = o.ElementType
default:
return false
}
case TSlice:
switch o.Type {
case TNil:
// nil instantiates an empty slice
i.IsConcrete = true
i.Bits = 0
i.MinBits = 0
i.ArraySize = 0
return true
case TArray:
// Instantiating slice from an array. Continue below.
case TPtr:
if !o.ElementType.Type.Array() {
return false
}
// Instantiating slice from pointer to array
// i.e. continue below.
i.Type = TPtr
i.ElementType = o.ElementType
default:
return false
}
case TInt:
switch o.Type {
case TUint:
if o.MinBits < o.Bits {
// Unsigned integer not using all bits i.e. it is
// non-negative. We can use it as r-value for
// signed integer.
i.IsConcrete = true
i.Bits = o.Bits
i.MinBits = o.Bits
return true
}
}
return false
default:
return false
}
}
if i.Concrete() {
return false
}
switch i.Type {
case TStruct:
return false
case TArray, TSlice:
if !i.ElementType.Concrete() &&
!i.ElementType.Instantiate(*o.ElementType) {
return false
}
if i.ElementType.Type != o.ElementType.Type {
return false
}
i.IsConcrete = true
i.Bits = o.Bits
i.ArraySize = o.ArraySize
return true
case TPtr:
if i.ElementType.Type != o.ElementType.Type {
return false
}
i.IsConcrete = true
i.Bits = o.Bits
return true
default:
i.IsConcrete = true
i.Bits = o.Bits
return true
}
}
// InstantiateWithSizes creates a concrete type of the unspecified
// type with given element sizes.
func (i *Info) InstantiateWithSizes(sizes []int) error {
if len(sizes) == 0 {
return fmt.Errorf("not enought sizes for type %v", i)
}
switch i.Type {
case TBool:
case TInt, TUint, TFloat:
if !i.Concrete() {
i.Bits = Size(sizes[0])
}
case TStruct:
var structBits Size
for idx := range i.Struct {
if idx >= len(sizes) {
return fmt.Errorf("not enought sizes for type %v", i)
}
err := i.Struct[idx].Type.InstantiateWithSizes(sizes[idx:])
if err != nil {
return err
}
i.Struct[idx].Type.Offset = structBits
structBits += i.Struct[idx].Type.Bits
}
i.Bits = structBits
case TArray:
if !i.ElementType.Concrete() {
return fmt.Errorf("array element type unspecified: %v", i)
}
if !i.Concrete() {
i.ArraySize = Size(sizes[0]) / i.ElementType.Bits
if Size(sizes[0])%i.ElementType.Bits != 0 {
i.ArraySize++
}
i.Bits = i.ArraySize * i.ElementType.Bits
}
case TSlice:
if !i.ElementType.Concrete() {
return fmt.Errorf("slice element type unspecified: %v", i)
}
i.ArraySize = Size(sizes[0]) / i.ElementType.Bits
if Size(sizes[0])%i.ElementType.Bits != 0 {
i.ArraySize++
}
i.Bits = i.ArraySize * i.ElementType.Bits
default:
return fmt.Errorf("can't specify %v", i)
}
i.SetConcrete(true)
return nil
}
// Equal tests if the argument type is equal to this type info.
func (i Info) Equal(o Info) bool {
if i.Type != o.Type {
return false
}
switch i.Type {
case TUndefined, TBool, TInt, TUint, TFloat, TString:
return i.Bits == o.Bits
case TStruct:
if len(i.Struct) != len(o.Struct) || i.Bits != o.Bits {
return false
}
for idx, ie := range i.Struct {
if !ie.Type.Equal(o.Struct[idx].Type) {
return false
}
}
return true
case TArray, TSlice:
if i.ArraySize != o.ArraySize || i.Bits != o.Bits {
return false
}
return i.ElementType.Equal(*o.ElementType)
case TPtr:
return i.ElementType.Equal(*o.ElementType)
default:
panic(fmt.Sprintf("Info.Equal called for %v (%T)", i.Type, i.Type))
}
}
// Specializable tests if this type can be specialized with the
// argument type.
func (i Info) Specializable(o Info) bool {
if i.Type != o.Type {
return false
}
switch i.Type {
case TUndefined, TBool, TInt, TUint, TFloat, TString:
return !i.Concrete() || i.Bits == o.Bits
case TStruct:
if len(i.Struct) != len(o.Struct) ||
(i.Concrete() && i.Bits != o.Bits) {
return false
}
for idx, ie := range i.Struct {
if !ie.Type.Specializable(o.Struct[idx].Type) {
return false
}
}
return true
case TArray:
if i.Concrete() && (i.ArraySize != o.ArraySize || i.Bits != o.Bits) {
return false
}
return i.ElementType.Specializable(*o.ElementType)
case TSlice:
return i.ElementType.Specializable(*o.ElementType)
case TPtr:
return i.ElementType.Specializable(*o.ElementType)
default:
panic(fmt.Sprintf("Info.Specializable called for %v (%T)",
i.Type, i.Type))
}
}
// CanAssignConst tests if the argument const type can be assigned to
// this type.
func (i Info) CanAssignConst(o Info) bool {
switch i.Type {
case TInt, TUint:
return (o.Type == TInt || o.Type == TUint) && i.Bits >= o.MinBits
case TSlice:
return o.Type.Array() && i.ElementType.Equal(*o.ElementType)
case TArray:
if o.Type == TNil {
return true
}
if !o.Type.Array() || !i.ElementType.Equal(*o.ElementType) {
return false
}
return i.Bits >= o.MinBits
default:
return i.Type == o.Type && i.Bits >= o.MinBits
}
}