Skip to content

Commit ad2d339

Browse files
KJ-11 Eliminate the rule [runtime-elabExpressions]
Implemented, awaits confirmation by jenkins tests.
1 parent d36e474 commit ad2d339

12 files changed

Lines changed: 93 additions & 106 deletions

semantics/api-core.k

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ when
187187

188188
rule [methodInvokeRuntime-String-equals]:
189189
methodInvokeRuntime(rtString, sig(MethodName:Id, _), ThisStr:String::_, ParamStr:String::_ ,_ )::_
190-
=> 'Eq(ThisStr,, ParamStr)
190+
=> elab('Eq(ThisStr,, ParamStr))
191191
when Id2String(MethodName) ==String "equals"
192192
193193
//@String.toString()
@@ -213,7 +213,7 @@ when
213213
214214
rule [methodInvokeRuntime-String-compareTo]:
215215
methodInvokeRuntime(rtString, sig(MethodName:Id, _), ThisStr:String::_, ParamStr:String::_ ,_ )::_
216-
=> 'If(ThisStr <String ParamStr,, -1,, 'If(ThisStr ==String ParamStr,, 0,, 1))
216+
=> elab('If(ThisStr <String ParamStr,, -1,, 'If(ThisStr ==String ParamStr,, 0,, 1)))
217217
when Id2String(MethodName) ==String "compareTo"
218218
219219
//@\subsection{Array clone}

semantics/api-threads.k

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ rule [thread-start]:
2727
<thread>
2828
<k>
2929
//typedLookup(OL).runnable.run()
30-
'ExprStm(
30+
elab('ExprStm(
3131
'Invoke(
3232
'Method(
3333
'MethodName(
@@ -40,7 +40,7 @@ rule [thread-start]:
4040
),,
4141
'ListWrap(.KList)
4242
)
43-
)
43+
))
4444
</k>
4545
<tid> TId </tid>
4646
...
@@ -78,12 +78,12 @@ rule [Synchronized-nested]:
7878
7979
rule [Synchronized-on-null]:
8080
'Synchronized(null::_,,_)
81-
=> 'Throw('NewInstance(
81+
=> elab('Throw('NewInstance(
8282
'None(.KList),,
8383
class String2Id("java.lang.NullPointerException"),,
8484
'ListWrap( null ),,
8585
'None(.KList)
86-
))
86+
)))
8787
8888
/*@Release one level of lock for the given object.*/
8989
syntax K ::= "releaseLock" "(" Int ")"
@@ -137,12 +137,12 @@ rule [thread-join-interrupted]:
137137
methodClosure(Class:ClassType,_,_,_,_,_, 'NoMethodBody(_))::methodType(sig(MethodName:Id, _),_), _,
138138
'ListWrap(TId:Int :: _)
139139
)
140-
=> 'Throw('NewInstance(
140+
=> elab('Throw('NewInstance(
141141
'None(.KList),,
142142
class String2Id("java.lang.InterruptedException"),,
143143
'ListWrap( null ),,
144144
'None(.KList)
145-
))
145+
)))
146146
...
147147
</k>
148148
<interrupted> true => false </interrupted>
@@ -178,12 +178,12 @@ rule [object-wait-notify-notifyAll-without-sync]:
178178
methodClosure(Class:ClassType,_,_,_,_,_, 'NoMethodBody(_))::methodType(sig(MethodName:Id, _),_),
179179
OL:Int, 'ListWrap(.KList)
180180
)
181-
=> 'Throw('NewInstance(
181+
=> elab('Throw('NewInstance(
182182
'None(.KList),,
183183
class String2Id("java.lang.IllegalMonitorStateException"),,
184184
'ListWrap( null ),,
185185
'None(.KList)
186-
))
186+
)))
187187
...
188188
</k>
189189
<holds> Holds:Map </holds>
@@ -218,12 +218,12 @@ when
218218
rule [waitImpl-interrupted]:
219219
<k>
220220
waitImpl(OL:Int)
221-
=> 'Throw('NewInstance(
221+
=> elab('Throw('NewInstance(
222222
'None(.KList),,
223223
class String2Id("java.lang.InterruptedException"),,
224224
'ListWrap( null ),,
225225
'None(.KList)
226-
))
226+
)))
227227
...
228228
</k>
229229
<tid> TId:Int </tid>

semantics/arrays.k

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ rule [ArrayVarDecIdDiscard]:
3434
rule [VarDecWithArrayInitDesugar]:
3535
'LocalVarDec(_:K,, T:Type,, 'ListWrap('VarDec(
3636
X:Id,,
37-
( 'ArrayInit(InitK:K) => 'NewArray(T,, 'ListWrap(.KList),, 'ArrayInit(InitK)) )
37+
( 'ArrayInit(InitK:K) => elab('NewArray(T,, 'ListWrap(.KList),, 'ArrayInit(InitK))) )
3838
))) [structural, anywhere]
3939

4040
//same three rules for fields
@@ -58,7 +58,7 @@ rule [ArrayFieldDecIdDiscard]:
5858
rule [FieldDecWithArrayInitDesugar]:
5959
'FieldDec(_:K,, T:Type,, 'ListWrap('VarDec(
6060
X:Id,,
61-
( 'ArrayInit(InitK:K) => 'NewArray(T,, 'ListWrap(.KList),, 'ArrayInit(InitK)) )
61+
( 'ArrayInit(InitK:K) => elab('NewArray(T,, 'ListWrap(.KList),, 'ArrayInit(InitK))) )
6262
))) [structural]
6363

6464
//first two rules - desugaring C-like array declarations, for params
@@ -104,28 +104,28 @@ rule [lookupArrayLocation]:
104104

105105
rule [ArrayIndexOutOfBoundsException]:
106106
'ArrayAccess(arrayRef(_,_,M:Int) :: _,, N:Int::_)::_
107-
=> 'Throw('NewInstance(
107+
=> elab('Throw('NewInstance(
108108
'None(.KList),,
109109
(class String2Id("java.lang.ArrayIndexOutOfBoundsException")),,
110110
'ListWrap( Int2String(N) ),,
111111
'None(.KList)
112-
))
112+
)))
113113
when notBool ((N >=Int 0) andBool (N <Int M))
114114
[structural, anywhere]
115115

116-
//Required to desugar effects of the previous rule, when exception happens
117-
//inside lvalue.
116+
/*@ Required when a JVM-related exception (e.g. produced by the semantics at runtime) is thrown inside a lvalue.
117+
*/
118118
rule [LvalueIndexOutOfBoundsDesugar]:
119-
lvalue('Throw(K:K)) => 'Throw(K)
119+
lvalue(elab('Throw(K:K))) => elab('Throw(K))
120120

121121
rule [arrayAccessNull]:
122122
'ArrayAccess(null::_ ,, _)::_
123-
=> 'Throw('NewInstance(
123+
=> elab('Throw('NewInstance(
124124
'None(.KList),,
125125
class String2Id("java.lang.NullPointerException"),,
126126
'ListWrap( null ),,
127127
'None(.KList)
128-
))
128+
)))
129129
[structural, anywhere]
130130

131131
//Array length, as defined in JDK
@@ -174,12 +174,12 @@ when NI >=Int 0
174174

175175
rule [NegativeArraySizeException]:
176176
checkNonNegative((NI:Int::_),, _)
177-
=> 'Throw('NewInstance(
177+
=> elab('Throw('NewInstance(
178178
'None(.KList),,
179179
class String2Id("java.lang.NegativeArraySizeException"),,
180180
'ListWrap( null ),,
181181
'None(.KList)
182-
))
182+
)))
183183
when
184184
NI <Int 0
185185
@@ -279,7 +279,7 @@ rule [assignImpl]:
279279
280280
rule [ArrayStoreException]:
281281
assignImpl( false::bool, _, TV:TypedVal)
282-
=> 'Throw('NewInstance(
282+
=> elab('Throw('NewInstance(
283283
'None(.KList),,
284284
(class String2Id("java.lang.ArrayStoreException")),,
285285
'ListWrap(
@@ -295,7 +295,7 @@ rule [ArrayStoreException]:
295295
)
296296
),,
297297
'None(.KList)
298-
))
298+
)))
299299

300300
//@ \subsection{Array initializer}
301301

semantics/classes.k

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ module CLASSES
2828
//@ \texttt{toString for objectClosure}
2929
3030
rule toString(objectClosure(I:Int, Obj:List, ObjLocalClassesEnv:Map) :: T:Type)
31-
=> 'Invoke(
31+
=> elab('Invoke(
3232
'MethodName(objectClosure(I, Obj, ObjLocalClassesEnv) :: T,, String2Id("toString")),,
3333
'ListWrap(.KList)
34-
)
34+
))
3535
[anywhere]
3636

3737
//@ \texttt{typeOf for reference types}
@@ -142,10 +142,10 @@ syntax K ::= "invokeConstr" "(" Int //OL - object location
142142
rule [invokeConstr]:
143143
invokeConstr(OL:Int, Class:ClassType, Vals:KList, EnclosingObj:KResult)
144144
=> setEncloser(lookup(OL)::Class, Class, EnclosingObj)
145-
~> 'ExprStm('Invoke(
145+
~> elab('ExprStm('Invoke(
146146
'Method('MethodName( lookup(OL)::Class,, getConsName(Class) )),,
147147
'ListWrap( Vals )
148-
))
148+
)))
149149

150150
rule [setEncloser-value]:
151151
<k>
@@ -249,13 +249,13 @@ rule [CastRef]:
249249
=> 'If(
250250
subtype(typeOf(V), RT1),,
251251
(V::RT1),,
252-
'Throw('NewInstance(
252+
elab('Throw('NewInstance(
253253
'None(.KList),,
254254
(class String2Id("java.lang.ClassCastException")),,
255255
'ListWrap( 'Plus('Plus(toString(typeOf(V)),, " cannot be cast to ")::rtString,,
256256
toString(RT1))::rtString ),,
257257
'None(.KList)
258-
))
258+
)))
259259
)
260260
...
261261
</k>

semantics/elaborate-blocks.k

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -547,17 +547,9 @@ rule [unwrapElabNaked]:
547547
when
548548
isElabNaked(K)
549549
550-
rule [elabCoolStatement]:
550+
rule [elabCool-elabRes]:
551551
(elabRes(K:K) => .) ~> elab(_:KLabel(_,, (CHOLE => elabRes(K)),, _))
552-
/*when
553-
notBool isElabIndependent(K)*/
554552
555-
/*rule [elabCoolExpressionOrKResult]:
556-
(elabRes(ElabIndep:K) => .) ~> elab(_:KLabel(_,, (CHOLE => ElabIndep),, _))
557-
when
558-
isElabIndependent(ElabIndep)*/
559-
560-
/*Was KR:ElabIndependent before, but in fact only KResult may stay free in this position.*/
561553
rule [elabCoolKResult]:
562554
(KR:KResult => .) ~> elab(_:KLabel(_,, (CHOLE => KR),, _))
563555
@@ -599,18 +591,6 @@ rule isElab(K:K,, Ks:KList)
599591

600592
rule isElab(.KList) => true
601593

602-
/*@ Either of:
603-
- KResult
604-
- TypedExp
605-
Used in defaut cooling rules of elaboration phase, to distinguish between elborated statements and elaborated
606-
expressions. When expressions are cooled they are unwrapped fron thei elabRes() wrapper.
607-
*/
608-
/*
609-
syntax K ::= "isElabIndependent" "(" K ")" [function]
610-
rule isElabIndependent(K:K)
611-
=> (isKResult(K) ==K true) orBool (isTypedExp(K) ==K true)
612-
*/
613-
614594
//@ True if given KList have no terms of the form elabRes(...), false otherwise.
615595
syntax K ::= "haveNoElabRes" "(" KList ")" [function]
616596
rule haveNoElabRes(K:K,, Ks:KList)
@@ -1287,24 +1267,17 @@ rule count(Ks:KList) => count(0, Ks)
12871267
rule count(I:Int, K:K,, Ks:KList) => count(I +Int 1, Ks)
12881268
rule count(I:Int, .KList) => I::int
12891269
1290-
//todo latex workaround: manual character escape: \%
1291-
1292-
//@ Elaborate all expressions once they reach the top of computation,
1293-
//@ both in elaboration and in execution phase.
1294-
//@ Performance: Using this rule instead of the rule above leads to execution performance decrease by 5\%
1295-
//@ in helloWorld, with medium execution time rising from 9.6s to 10.1s
1296-
rule [runtime-elabExpressions]:
1297-
KL:KLabel(Ks:KList) => elab(KL(Ks))
1298-
when
1299-
isExpressionLabel(KL)
1300-
13011270
//@ Unwrap elaborated terms in execution phase, so that they could be executed.
1302-
rule [elabResInExecutionPhase]:
1271+
rule [elabRes-ExecutionPhase-discard1]:
13031272
<k> (elabRes(K:K) => K) ~> KL:KLabel(_) ...</k>
1304-
<globalPhase> ExecutionPhase </globalPhase>
1273+
<globalPhase> ExecutionPhase </globalPhase>
13051274
when
13061275
KL =/=KLabel 'elab`(_`)
13071276

1277+
rule [elabRes-ExecutionPhase-discard2]:
1278+
<k> elabRes(K:K) => K </k>
1279+
<globalPhase> ExecutionPhase </globalPhase>
1280+
13081281
//@\subsection{Elaboration of SuperConstrInv, AltConstrInv}
13091282

13101283
//@ Desugaring unqualified superclass constructor invocation into a qualified one

semantics/expressions.k

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,25 @@ rule 'Div(F1:Float :: _,, F2:Float :: _) :: ResT:FloatType => (
118118
119119
//todo test that the first argument is fully evaluated, right now it may be not evaluated
120120
rule 'Div(_::_ ,, 0::_)::_
121-
=> 'Throw('NewInstance(
121+
=> elab('Throw('NewInstance(
122122
'None(.KList),,
123123
(class String2Id("java.lang.ArithmeticException")),,
124124
'ListWrap( "/ by zero" ),,
125125
'None(.KList)
126-
))
126+
)))
127127
128128
rule /* I1 % I2 */ 'Remain(I1:Int :: _,, I2:Int :: _) :: ResT:IntType => normalize( (I1 %Int I2) :: ResT )
129129
when I2 =/=Int 0
130130
rule 'Remain(F1:Float :: _,, F2:Float :: _) :: ResT:FloatType => (F1 %Float F2) :: ResT
131131
132132
//todo test that the first argument is fully evaluated, right now it may be not evaluated
133133
rule 'Remain(_::_ ,, 0::_)::_
134-
=> 'Throw('NewInstance(
134+
=> elab('Throw('NewInstance(
135135
'None(.KList),,
136136
(class String2Id("java.lang.ArithmeticException")),,
137137
'ListWrap( "/ by zero" ),,
138138
'None(.KList)
139-
))
139+
)))
140140
141141
rule [NormalizeLeftOperandToFloat]:
142142
KL:KLabel((I:Int::_:IntType => Int2Float(I)::float),, _:Float :: _:FloatType)::_

semantics/literals.k

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module LITERALS
88
imports CORE
99
imports AUX-STRINGS
1010
imports PRIMITIVE-TYPES
11+
imports ELABORATE-BLOCKS //for elab(), should disappear after semantics separation
1112

1213
//@ \subsection{Auxiliary constructs}
1314

@@ -152,9 +153,9 @@ rule 'Lit('Char('UnicodeEscape(_:K,, I1:Int,, I2:Int,, I3:Int,, I4:Int)))
152153
//@ \subsection{String literals}
153154
154155
rule 'Lit('String('ListWrap(K1:K,, K2:K,, Ks:KList)))
155-
=> 'Plus('Lit('String('ListWrap(K1))),,
156+
=> elab('Plus('Lit('String('ListWrap(K1))),,
156157
'Lit('String('ListWrap(K2,, Ks)))
157-
)
158+
))
158159
159160
rule 'Lit('String('ListWrap('Chars(Str:String)))) => Str
160161

semantics/method-invoke.k

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,12 @@ when
418418
419419
rule [methodInvokeRuntimeOnNull]:
420420
methodInvokeRuntime(_,_, null::_, ParamVals:KList, instanceCT)::_
421-
=> 'Throw('NewInstance(
421+
=> elab('Throw('NewInstance(
422422
'None(.KList),,
423423
class String2Id("java.lang.NullPointerException"),,
424424
'ListWrap( null ),,
425425
'None(.KList)
426-
))
426+
)))
427427
when
428428
isKResult(ParamVals)
429429

@@ -649,7 +649,7 @@ syntax K ::= "bindto" KList
649649
rule [bindto]:
650650
<k>
651651
bindto paramImpl(T:Type, X:Id),, RestP:KList, TV:TypedVal,, RestV:KList
652-
=> 'ExprStm('Assign(localVar(X)::T,,TV:TypedVal)) ~> bindto RestP, RestV
652+
=> elab('ExprStm('Assign(localVar(X)::T,,TV:TypedVal))) ~> bindto RestP, RestV
653653
...
654654
</k>
655655
<env> Env:Map => Env[L/X] </env>

semantics/process-local-classes.k

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ rule buildConstructorFirstLineArgs(.KList, Ks:KList, _) => resultListWrap(Ks)
225225
226226
rule [elabAnonymous-QNewInstance-extends-class]:
227227
<k>
228-
elab('QNewInstance(Qual:TypedExp,, Arg2:K,, Class:ClassType,, Arg4:K,,
228+
elab('QNewInstance(elabRes(Qual:TypedExp),, Arg2:K,, Class:ClassType,, Arg4:K,,
229229
elabRes('ListWrap(ActualArgs:KList)),,
230230
'Some('ClassBody('ListWrap(AnonClassDecs:KList)))
231231
))

0 commit comments

Comments
 (0)