forked from kframework/c-semantics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitsize.k
110 lines (89 loc) · 4.03 KB
/
bitsize.k
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
module C-BITSIZE-SYNTAX
imports C-DYNAMIC-SYNTAX
imports C-TYPING-SYNTAX
syntax Int ::= bitSizeofType(Type) [function]
syntax Int ::= byteSizeofType(Type) [function]
syntax Int ::= bitSizeofStruct(FieldInfo) [function]
syntax Int ::= bitSizeofUnion(FieldInfo) [function]
syntax KItem ::= sizeof(K) [strict]
syntax Int ::= maxByteSizeofList(List) [function]
endmodule
module C-BITSIZE
imports C-BITSIZE-SYNTAX
imports C-BITS-SYNTAX
imports C-ENV-SYNTAX
imports C-CONVERSION-SYNTAX
imports C-SYNTAX
imports C-ERROR-SYNTAX
imports C-SETTINGS-SYNTAX
imports COMPAT-SYNTAX
rule sizeof(V:KResult => type(V))
requires notBool isType(V)
[structural]
rule sizeof(T:Type => stabilizeVLA(T))
requires isVariableLengthArrayType(T)
[structural]
rule sizeof(T:Type)
=> Cast(t(SetItem(IntegerConstant), cfg:sizeut),
tv(byteSizeofType(T), t(SetItem(IntegerConstant), cfg:largestUnsigned)))
requires notBool isVariableLengthArrayType(T)
[structural]
rule byteSizeofType(T:Type)
=> bitsToBytes(bitSizeofType(T))
syntax Int ::= numBytes(Type) [function]
rule numBytes(t(_, bool)) => cfg:sizeofBool
rule numBytes(t(_, signed-char)) => cfg:sizeofSignedChar
rule numBytes(t(_, short-int)) => cfg:sizeofShortInt
rule numBytes(t(_, int)) => cfg:sizeofInt
rule numBytes(t(_, long-int)) => cfg:sizeofLongInt
rule numBytes(t(_, long-long-int)) => cfg:sizeofLongLongInt
rule numBytes(t(_, float)) => cfg:sizeofFloat
rule numBytes(t(_, double)) => cfg:sizeofDouble
rule numBytes(t(_, long-double)) => cfg:sizeofLongDouble
rule numBytes(t(_, unsigned-char)) => numBytes(t(.Set, signed-char))
rule numBytes(t(_, unsigned-short-int)) => numBytes(t(.Set, short-int))
rule numBytes(t(_, unsigned-int)) => numBytes(t(.Set, int))
rule numBytes(t(_, unsigned-long-int)) => numBytes(t(.Set, long-int))
rule numBytes(t(_, unsigned-long-long-int))
=> numBytes(t(.Set, long-long-int))
rule numBytes(t(S:Set, enumType(_))) => numBytes(t(S, cfg:enumAlias))
rule numBytes(_) => 0 [owise]
syntax Int ::= byteSizeofList(List) [function]
syntax Int ::= "byteSizeofList'" "(" Int "," List ")" [function]
rule byteSizeofList(L:List) => byteSizeofList'(0, L)
rule byteSizeofList'(Sz:Int, ListItem(T:Type) LL:List)
=> byteSizeofList'((Sz +Int byteSizeofType(T)), LL)
rule byteSizeofList'(Sz:Int, .List) => Sz
syntax Int ::= "maxByteSizeofList'" "(" Int "," List ")" [function]
rule maxByteSizeofList(L:List) => maxByteSizeofList'(0, L)
rule maxByteSizeofList'(Sz:Int, ListItem(T:Type) LL:List)
=> maxByteSizeofList'(maxInt(Sz, byteSizeofType(T)), LL)
rule maxByteSizeofList'(Sz:Int, .List) => Sz
rule bitSizeofType(t(_, no-type))
=> 0
rule bitSizeofType(t(_, arrayType(T:Type, N:Int)))
=> bitSizeofType(T) *Int N
rule bitSizeofType(t(_, flexibleArrayType(_)))
=> 0
rule bitSizeofType(t(_, functionType(_, _)))
=> cfg:bitsPerByte
rule bitSizeofType(t(_, pointerType(_)))
=> cfg:ptrsize *Int cfg:bitsPerByte
rule bitSizeofType(t(_, bitfieldType(_, N:Int)))
=> N
rule bitSizeofType(T:BasicType) => numBytes(T) *Int cfg:bitsPerByte
requires notBool isTypedDeclaration(T)
andBool notBool isBitfieldType(T)
rule bitSizeofType(typedDeclaration(T:Type, _))
=> bitSizeofType(T)
rule bitSizeofType(t(_, structType(S:StructId)))
=> bitSizeofStruct(getFieldInfo(S))
rule bitSizeofType(t(_, unionType(S:StructId)))
=> bitSizeofUnion(getFieldInfo(S))
rule bitSizeofStruct(fieldInfo(_, NBits:Int, _, _))
=> NBits
rule bitSizeofUnion(fieldInfo(_, NBits:Int, _, _))
=> NBits
syntax Int ::= bitsToBytes(Int) [function]
rule bitsToBytes(N:Int) => (absInt(N) +Int cfg:bitsPerByte -Int 1) /Int cfg:bitsPerByte
endmodule