Skip to content

Commit 24ff37a

Browse files
chathhornh0nzZik
authored andcommitted
C parser fixes needed for the LLVM backend (#578)
* LLVM backend: semantics fixes. * LLVM backend: parser fixes. * stdio.k: Fix default field width. * Fix for converting encoded values.
1 parent 39a810b commit 24ff37a

File tree

24 files changed

+220
-203
lines changed

24 files changed

+220
-203
lines changed

parser/kPrinter.ml

Lines changed: 78 additions & 56 deletions
Large diffs are not rendered by default.

semantics/c/language/common/conversion.k

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ module C-CONVERSION
8181
requires notBool isBoolUType(T')
8282
andBool M -Int N <=Int bitSizeofType(T')
8383

84+
rule cast(integerUType #as T'::UType, tv(encodedValue(V::EncodableValue, N::Int, M::Int), integerUType))
85+
=> tv(encodedValue(V, N, M), T')
86+
requires notBool isBoolUType(T')
87+
andBool M -Int N <=Int bitSizeofType(T')
88+
8489
/*@ \fromStandard{\source[n1570]{\para{6.3.1.3}{2}}}{
8590
Otherwise, if the new type is unsigned, the value is converted by
8691
repeatedly adding or subtracting one more than the maximum value that can

semantics/c/language/common/dynamic.k

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ module C-DYNAMIC-SYNTAX
4141

4242
// These hold typed frozen computations -- one each for lvalues, rvalues,
4343
// and non-converted lvalues.
44-
syntax LHold ::= le(K, Type)
45-
syntax RHold ::= te(K, UType)
46-
syntax NCLHold ::= ncle(K, Type)
44+
syntax LHold ::= le(KItem, Type)
45+
syntax RHold ::= te(KItem, UType)
46+
syntax NCLHold ::= ncle(KItem, Type)
4747
syntax CompoundExpression ::= compoundExp(K)
4848
syntax Hold ::= HoldLValue | HoldResult | CompoundExpression
4949

50-
syntax K ::= stripHold(K) [function]
50+
syntax KItem ::= stripHold(KItem) [function]
5151

5252
syntax RValue ::= "voidVal" [klabel(voidVal)]
5353
syntax RValue ::= "emptyValue"
@@ -93,8 +93,7 @@ module C-DYNAMIC-SYNTAX
9393
syntax KItem ::= "comma"
9494
// these are semantic
9595

96-
syntax KResult ::= initValue(CId, Type, K)
97-
syntax NoInit
96+
syntax NoInit ::= initValue(CId, Type, K)
9897
syntax KResult ::= NoInit
9998

10099
syntax CompoundLiteralId ::= compoundLiteral(Int)
@@ -168,18 +167,18 @@ module C-DYNAMIC
168167

169168
context toRVal(HOLE:KItem => reval(HOLE)) [result(RValue)]
170169

171-
rule stripHold(te(K:K, _)) => K
172-
rule stripHold(le(K:K, _)) => K
173-
rule stripHold(ncle(K:K, _)) => K
170+
rule stripHold(te(K:KItem, _)) => K
171+
rule stripHold(le(K:KItem, _)) => K
172+
rule stripHold(ncle(K:KItem, _)) => K
174173
rule stripHold(tv(_, _) #as V::RValue) => checkUse(V)
175174
rule stripHold(nclv(Loc::SymLoc, T::Type)) => lv(Loc, T)
176-
rule stripHold(K:K) => K [owise]
175+
rule stripHold(K:KItem) => K [owise]
177176

178177
rule value(tv(V:CValue, _)) => V
179178
rule value(nclv(Loc:SymLoc, _)) => Loc
180-
rule value(te(K:K, _)) => K
181-
rule value(le(K:K, _)) => K
182-
rule value(ncle(K:K, _)) => K
179+
rule value(te(K:KItem, _)) => K
180+
rule value(le(K:KItem, _)) => K
181+
rule value(ncle(K:KItem, _)) => K
183182
rule value(compoundExp(K:K)) => K
184183

185184
rule type(tv(_, T::UType)) => type(T)
@@ -209,8 +208,6 @@ module C-DYNAMIC
209208
rule N:Int => tv(N, utype(cfg:largestUnsigned))
210209
[structural] // for internal computations
211210

212-
rule isNoInit(initValue(_, _, .K)) => true
213-
214211
rule fillRHoles(V:RValue, te(L:K := E:K, T::UType)) => te(L := fillRHoles(V, E), T)
215212
rule fillRHoles(V:RValue, Cast(T::Type, E:K)) => Cast(T, fillRHoles(V, E))
216213
rule fillRHoles(V:RValue, RHOLE) => V

semantics/c/language/common/expr/relational.k

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -444,27 +444,16 @@ module C-COMMON-EXPR-RELATIONAL
444444
requires Loc =/=K NullPointer
445445
andBool Loc' =/=K NullPointer
446446
andBool notBool sameBase(Loc, Loc')
447-
andBool notBool isAdjacent(stripProv(Loc), stripProv(Loc'))
448447
andBool isCompatibleEqualityTypes(T, T', Loc, Loc')
449448
[structural]
450449
rule tv(Loc:SymLoc, T::UType) != tv(Loc':SymLoc, T'::UType)
451450
=> makeTruth(true, T, T')
452451
requires Loc =/=K NullPointer
453452
andBool Loc' =/=K NullPointer
454453
andBool notBool sameBase(Loc, Loc')
455-
andBool notBool isAdjacent(stripProv(Loc), stripProv(Loc'))
456454
andBool isCompatibleEqualityTypes(T, T', Loc, Loc')
457455
[structural]
458456

459-
rule tv(Loc:SymLoc, T::UType) == tv(Loc':SymLoc, T'::UType)
460-
=> tv(unknown(1), makeTruthType(T, T'))
461-
requires isAdjacent(stripProv(Loc), stripProv(Loc'))
462-
andBool isCompatibleEqualityTypes(T, T', Loc, Loc')
463-
rule tv(Loc:SymLoc, T::UType) != tv(Loc':SymLoc, T'::UType)
464-
=> tv(unknown(0), makeTruthType(T, T'))
465-
requires isAdjacent(stripProv(Loc), stripProv(Loc'))
466-
andBool isCompatibleEqualityTypes(T, T', Loc, Loc')
467-
468457
// arithmetic types
469458
rule isCompatibleEqualityTypes(T1::UType, T2::UType, _, _) => true
470459
requires (isPromoted(T1) orBool isFloatUType(T1))
@@ -498,11 +487,4 @@ module C-COMMON-EXPR-RELATIONAL
498487
andBool notBool (isPointerType(type(L)) andBool isNullPointerConstant(R))
499488
andBool notBool (isPointerType(type(R)) andBool isNullPointerConstant(L))
500489

501-
syntax Bool ::= isAdjacent(SymLoc, SymLoc) [function, withConfig]
502-
rule [[ isAdjacent(loc(S::SymBase, Size:Int), loc(_, 0)) => true ]]
503-
<mem>... S |-> object(_, Size, _) ...</mem>
504-
505-
rule [[ isAdjacent(loc(_, 0), loc(S::SymBase, Size:Int)) => true ]]
506-
<mem>... S |-> object(_, Size, _) ...</mem>
507-
rule isAdjacent(_, _) => false [owise]
508490
endmodule

semantics/c/language/common/memory/reading.k

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ module C-MEMORY-READING
171171
requires NumBits >Int 0
172172
rule extractBitsFromList-aux(_, 0, 0, Done::List) => dataList(Done)
173173

174-
syntax KItem ::= interpret(UType, BitValue)
174+
syntax RValue ::= interpret(UType, BitValue) [function]
175175
rule interpret(integerUType #as T::UType, I:Int)
176176
=> #if max(T) >=Int I #then tv(I, T) #else interpret(T, I -Int (1 <<Int absInt(bitSizeofType(T)))) #fi
177177
rule interpret(integerUType #as T::UType, encodedPtr(NullPointer, _, _)) => tv(unknown(0), T)
178-
rule <k> interpret(T::UType, I:Int) => interpretPointer(T, I, Mem) ...</k>
178+
rule [[ interpret(T::UType, I:Int) => interpretPointer(T, I, Mem) ]]
179179
<mem> Mem::Map </mem>
180180
requires isPointerUType(T)
181181
rule interpret(ut(... st: _:SimpleFloatType) #as T::UType, 0) => tv(zeroCFloat(T), T)

semantics/c/language/common/syntax.k

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ endmodule
66

77
module C-SYNTAX
88
imports C-SORTS
9+
imports COMPAT-SORTS
910
imports C-REVAL-SYNTAX
1011
imports INT-SYNTAX
1112
imports STRING-SYNTAX
1213
imports COMMON-SYNTAX
1314

1415
syntax KResult ::= SpecifierElem
1516

16-
syntax KItem ::= Block(Int, KItem) [avoid]
17+
syntax KItem ::= Block(Int, StrictList) [avoid]
1718

1819
/*
1920
type cabsloc = {
@@ -263,7 +264,7 @@ module C-SYNTAX
263264
should be printed as just T *)
264265
| CALL of expression * expression list
265266
*/
266-
syntax KItem ::= Call(K, K) [symbol]
267+
syntax KItem ::= Call(KItem, KItem) [symbol]
267268
context Call((HOLE:KItem => reval(HOLE)), _) [ndheat, result(RValue)]
268269
/*
269270
| MEMBEROF of expression * string
@@ -276,6 +277,6 @@ module C-SYNTAX
276277
and attribute = String * expression list
277278
*/
278279
// String, List
279-
syntax KItem ::= Attribute(String, KItem) [symbol]
280+
syntax KItem ::= Attribute(String, StrictList) [symbol]
280281

281282
endmodule

semantics/c/language/common/type-builder.k

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ module C-TYPE-BUILDER
6767
rule restrict(T:FlexType) => addQualifier(Restrict(), expand(T))
6868

6969
rule typeref(S:String) => #typeref(Identifier(S))
70-
syntax Type ::= #typeref(CId) [function, withConfig]
7170

71+
syntax Type ::= #typeref(CId) [function, withConfig]
7272
rule [[ #typeref(X:CId) => T ]]
7373
<external-defs>... X |-> Base:SymBase ...</external-defs>
7474
<mem>... Base |-> object(T:Type, _, _) ...</mem>

semantics/c/language/common/typing/common.k

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ module C-TYPING-COMMON
121121
rule hasNoName(_) => false [owise]
122122

123123
rule [[ getTagInfo(tag(X::CId, Tu::String, B::BlockRef))
124-
=> extractTagInfo({M[Tu] orDefault .Map}:>Map[tag(X, Tu, B)]) ]]
124+
=> extractTagInfo({M[Tu] orDefault .Map}:>Map[tag(X, Tu, B)] orDefault #incomplete) ]]
125125
<global>... <tags> M::Map </tags> ...</global>
126126

127127
syntax TagInfo ::= extractTagInfo(K) [function]

semantics/c/language/common/typing/effective.k

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module C-TYPING-EFFECTIVE
6464
requires Loc =/=K NullPointer
6565
rule updateLastAccessVariant(Loc::SymLoc, _, _) => Loc [owise]
6666

67-
rule <k> getLastAccessType(loc(Base::SymBase, _, _:Set SetItem(objectType(T::Type))))
67+
rule <k> getLastAccessType(loc(Base::SymBase, _, SetItem(objectType(T::EffectiveType)) _::Set))
6868
=> fixFlexibleType(T, Len)
6969
...</k>
7070
<mem>... Base |-> object(_, Len::Int, _) ...</mem>

semantics/c/language/execution/init.k

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module C-EXECUTION-INIT-SYNTAX
55
imports COMMON-SORTS
66
syntax KItem ::= pgmArgs(List) [function]
77
syntax KItem ::= incomingArguments(List)
8-
syntax KItem ::= callEntryPoint(String, Int, K)
8+
syntax KItem ::= callEntryPoint(String, Int, KItem)
99
syntax KItem ::= initMainThread(String)
1010
syntax KItem ::= "initMainThread'" "(" String ")"
1111
syntax KItem ::= "callAtExit" | "callAtQuickExit"
@@ -85,23 +85,21 @@ module C-EXECUTION-INIT
8585
=> tv(NullPointer, utype(pointerType(type(void))))
8686

8787
syntax Type ::= strType(String) [function]
88-
rule strType(S:String)
88+
rule strType(S::String)
8989
=> type(arrayType(type(char), lengthString(S) +Int 1))
9090

91-
syntax KItem ::= "incomingArguments-aux" "(" List "," Int ")"
92-
93-
rule incomingArguments(L:List) => incomingArguments-aux(L, 0)
91+
rule incomingArguments(L::List) => #incomingArguments(L, 0)
9492
[structural]
95-
rule incomingArguments-aux(ListItem(S:String) L:List, N:Int)
93+
94+
syntax KItem ::= #incomingArguments(List, Int)
95+
rule #incomingArguments(ListItem(S:String) L::List, N::Int)
9696
=> allocObject(bnew(N +Int 1, strType(S), argv), strType(S))
9797
~> writeString(lnew(bnew(N +Int 1, strType(S), argv)), S)
98-
~> Initializer(*(mainArguments + tv(N, utype(int)))
99-
:= lv(lnew(bnew(N +Int 1, strType(S), argv)), strType(S)))
100-
~> incomingArguments-aux(L:List, N:Int +Int 1)
98+
~> Initializer(*(mainArguments + tv(N, utype(int))) := lv(lnew(bnew(N +Int 1, strType(S), argv)), strType(S)))
99+
~> #incomingArguments(L, N +Int 1)
101100
[structural]
102-
rule incomingArguments-aux(.List, N:Int)
103-
=> Initializer(*(mainArguments + tv(N, utype(int)))
104-
:= NullPointerConstant)
101+
rule #incomingArguments(.List, N::Int)
102+
=> Initializer(*(mainArguments + tv(N, utype(int))) := NullPointerConstant)
105103
[structural]
106104

107105
rule pgmArgs(L:List)
@@ -193,9 +191,9 @@ module C-EXECUTION-INIT
193191

194192
/*@ this bit of indirection is used to check that the main prototype is
195193
correct, and to call it with the appropriate arguments */
196-
rule <k> callEntryPoint(EP::String, N::Int, Args:K)
194+
rule <k> callEntryPoint(EP::String, N::Int, Args::KItem)
197195
=> enterRestrictTU
198-
~> reval(#callEntryPoint(EP, N, mainArguments, Args, T))
196+
~> reval(#callEntryPoint(EP, N, Args, T))
199197
~> callAtExit
200198
...</k>
201199
<types>... Identifier(EP) |-> T::Type ...</types>
@@ -206,14 +204,14 @@ module C-EXECUTION-INIT
206204
rule getEntryPoint(_) => "main" [owise]
207205

208206
// int main(void) -- also, int main() gets normalized to main(void)
209-
syntax K ::= #callEntryPoint(String, Int, CId, K, Type) [function]
210-
rule #callEntryPoint(Entry::String, _, _, _,
207+
syntax K ::= #callEntryPoint(String, Int, KItem, Type) [function]
208+
rule #callEntryPoint(Entry::String, _, _,
211209
t(quals(.Set), _, functionType(ut(.Set, int),
212210
ListItem(typedDeclaration(t(quals(.Set), .Set, void), _)))))
213211
=> Call(Identifier(Entry), list(.List))
214212
// int main(int argc, char* argv[]), int main(int argc, char** argv), and old style (no prototype).
215-
rule #callEntryPoint(Entry::String, N::Int, X::CId, Args:K, _)
216-
=> Args ~> Call(Identifier(Entry), list(ListItem(tv(N, utype(int))) ListItem(X)))
213+
rule #callEntryPoint(Entry::String, N::Int, Args::KItem, _)
214+
=> Args ~> Call(Identifier(Entry), list(ListItem(tv(N, utype(int))) ListItem(mainArguments)))
217215
[owise]
218216

219217
rule <k> reval(V:RValue) ~> callAtExit => AtExit ~> reval(V) ~> callAtExit ...</k>
@@ -231,8 +229,7 @@ module C-EXECUTION-INIT
231229
<status> _ => mainExited </status>
232230

233231
// fixme I'm not sure threads clean up their memory
234-
rule [terminate]:
235-
<exec>...
232+
rule <exec>...
236233
(<thread>...
237234
// main's thread, not the global "thread"
238235
<thread-id> 0 </thread-id>

0 commit comments

Comments
 (0)