Skip to content

Commit ff41ce0

Browse files
committed
[PEx] Correct inlining functions with non-null return without assignment, support test driver name same as test machine name
[PEx IR] Adds support for inlining functions that return non-null but are not assigned to a variable during function call [PEx IR+RT] Prefixes test driver names with test_ to support test name same as test main machine name [Tst] Adds unit test with test case name same as main machine name
1 parent f6b4c95 commit ff41ce0

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

Src/PCompiler/CompilerCore/Backend/PEx/CompilationContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ internal string GetNameForDecl(IPDecl decl)
7777
return $"{pEvent.Name}";
7878
return $"_{pEvent.Name}";
7979
case SafetyTest safety:
80-
return $"{safety.Name}";
80+
return $"test_{safety.Name}";
8181
default:
8282
throw new NotImplementedException($"decl type {decl.GetType().Name} not supported");
8383
}

Src/PCompiler/CompilerCore/Backend/PEx/PExCodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private void WriteMainDriver(CompilationContext context, StringWriter output, Sc
203203
return;
204204

205205
context.WriteLine(output, "@Generated");
206-
context.WriteLine(output, "public static class DefaultImpl extends PTestDriver {");
206+
context.WriteLine(output, "public static class test_DefaultImpl extends PTestDriver {");
207207
WriteDriver(context, output, mainMachine.Name, decls);
208208
context.WriteLine(output, "}");
209209
context.WriteLine(output);

Src/PCompiler/CompilerCore/Backend/PEx/TransformASTPass.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,10 @@ private static List<IPStmt> ReplaceReturn(IReadOnlyList<IPStmt> body, IPExpr loc
220220
switch (stmt)
221221
{
222222
case ReturnStmt returnStmt:
223-
newBody.Add(new AssignStmt(returnStmt.SourceLocation, location, returnStmt.ReturnValue));
223+
if (location != null)
224+
{
225+
newBody.Add(new AssignStmt(returnStmt.SourceLocation, location, returnStmt.ReturnValue));
226+
}
224227
newBody.Add(new AssignStmt(returnStmt.SourceLocation,
225228
new VariableAccessExpr(returnStmt.SourceLocation, inVar),
226229
new BoolLiteralExpr(returnStmt.SourceLocation, false)));
@@ -329,7 +332,20 @@ private static void InlineStmt(Function function, IPStmt stmt, List<IPStmt> body
329332
var inlined = InlineInFunction(call.Function);
330333
if (inlined)
331334
function.RemoveCallee(call.Function);
332-
GenerateInline(function, call.Function, call.ArgsList, body, call.SourceLocation);
335+
var appendToBody = new List<IPStmt>();
336+
var inVar = GenerateInline(function, call.Function, call.ArgsList, appendToBody,
337+
call.SourceLocation);
338+
body.Add(new AssignStmt(call.SourceLocation,
339+
new VariableAccessExpr(call.SourceLocation, inVar),
340+
new BoolLiteralExpr(call.SourceLocation, true)));
341+
appendToBody = ReplaceReturn(appendToBody, null, inVar, call.Function);
342+
foreach (var statement in appendToBody)
343+
{
344+
var inCond = new BinOpExpr(statement.SourceLocation, BinOpType.Eq,
345+
new VariableAccessExpr(statement.SourceLocation, inVar),
346+
new BoolLiteralExpr(statement.SourceLocation, true));
347+
body.Add(new IfStmt(statement.SourceLocation, inCond, statement, null));
348+
}
333349
}
334350
else
335351
{

Src/PRuntimes/PExRuntime/src/main/java/pex/PEx.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private static void setTestDriver(Reflections reflections)
142142
Set<Class<? extends PTestDriver>> subTypesDriver = reflections.getSubTypesOf(PTestDriver.class);
143143
PTestDriver driver = null;
144144
for (Class<? extends PTestDriver> td : subTypesDriver) {
145-
if (td.getSimpleName().equals(name)) {
145+
if (PTestDriver.getTestName(td).equals(name)) {
146146
driver = td.getDeclaredConstructor().newInstance();
147147
break;
148148
}
@@ -163,15 +163,15 @@ private static void setTestDriver(Reflections reflections)
163163
subTypesDriver.size()));
164164
PExLogger.logInfo("Possible options are:");
165165
for (Class<? extends PTestDriver> td : subTypesDriver) {
166-
PExLogger.logInfo(String.format("%s", td.getSimpleName()));
166+
PExLogger.logInfo(String.format("%s", PTestDriver.getTestName(td)));
167167
}
168168
if (!name.equals(defaultTestDriver)) {
169169
throw new Exception("No test driver found named \"" + PExGlobal.getConfig().getTestDriver() + "\"");
170170
} else {
171171
System.exit(6);
172172
}
173173
}
174-
PExGlobal.getConfig().setTestDriver(driver.getClass().getSimpleName());
174+
PExGlobal.getConfig().setTestDriver(PTestDriver.getTestName(driver.getClass()));
175175
PExGlobal.getModel().setTestDriver(driver);
176176
}
177177

Src/PRuntimes/PExRuntime/src/main/java/pex/runtime/machine/PTestDriver.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public PTestDriver() {
2929
configure();
3030
}
3131

32+
/**
33+
* Get test driver name
34+
*/
35+
public static String getTestName(Class<? extends PTestDriver> td) {
36+
String result = td.getSimpleName();
37+
if (result.startsWith("test_"))
38+
return result.substring("test_".length());
39+
return result;
40+
}
41+
3242
/**
3343
* Get the start/main machine of this test driver.
3444
*
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// P semantics XYZ: two machines, announce announce instantiation parameter
2+
// This is validation XYZ for announceInvocation.p
3+
event E2 assert 1: bool;
4+
5+
machine Main {
6+
var XYZ: bool;
7+
var ev2: event;
8+
start state Real1_Init {
9+
entry {
10+
ev2 = E2;
11+
announce ev2, XYZ; //"null event" error in Zing
12+
}
13+
}
14+
}
15+
spec M observes E2 {
16+
start state x {
17+
on E2 do (payload: bool) { assert (payload == false); }
18+
}
19+
}
20+
21+
test Main [main=Main]: assert M in { Main };
22+

0 commit comments

Comments
 (0)