Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6be2b01
Add global constant varaibles; TODO backend
zhezhouzz Jun 11, 2024
82637c6
Updated BuildGlobalScope
zhezhouzz Jun 11, 2024
7b55329
add global variable
zhezhouzz Jun 12, 2024
8cfe5c9
fix global variable types
zhezhouzz Jun 12, 2024
d630b9f
Allow multiple tests
zhezhouzz Jun 12, 2024
f39c431
Add int list literal syntax
zhezhouzz Jun 13, 2024
246d1d6
add parametric test
zhezhouzz Jun 13, 2024
71b519b
Quick-fix
zhezhouzz Jun 13, 2024
740de07
fix bad quick-fix
zhezhouzz Jun 13, 2024
5def42a
add rich syntax
zhezhouzz Jun 13, 2024
38dbf6d
Merge branch 'master' into paramtest
zhezhouzz Jun 17, 2024
08ee970
Merge branch 'mainp' into paramtest
zhezhouzz Jul 16, 2024
57d7c4b
Merge branch 'paramtest' of github.com:p-org/P-TestGeneration into pa…
zhezhouzz Jul 16, 2024
1a9754d
Merge branch 'master' into paramtest
zhezhouzz Aug 21, 2024
50463f5
Merge branch 'master' into paramtest
ankushdesai Dec 4, 2024
658c304
Merge branch 'paramtest' of https://github.com/p-org/P-TestGeneration…
ankushdesai Dec 4, 2024
924eced
Merge branch 'p-org-paramtest' into dev_p3.0/param_testcases
ankushdesai Dec 4, 2024
60d3fb4
Small merge error
ankushdesai Dec 4, 2024
bd3d167
Param (#833)
zhezhouzz Mar 19, 2025
c7e86ab
T-wise combinatorial test (#837)
zhezhouzz Mar 31, 2025
7a53857
Solved Concerns from Lewis:
zhezhouzz Mar 31, 2025
bf539a8
add new test case
zhezhouzz Mar 31, 2025
6356b7c
unit test for ParamAssignment.IterateAssignments
zhezhouzz Apr 1, 2025
0a3a47e
Merge branch 'porigin/dev_p3.0/param_testcases' into pex-param-test
zhezhouzz Apr 30, 2025
c2b30bc
merge
zhezhouzz May 1, 2025
35386ac
fix duplicate declears bug
zhezhouzz May 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

2 changes: 2 additions & 0 deletions Src/PCompiler/CompilerCore/Backend/ICodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Plang.Compiler.Backend
{
public interface ICodeGenerator
{
public const string GlobalConfigName = "GlobalConfig";

/// <summary>
/// Generate target language source files from a P project.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public class PCheckerCodeGenerator : ICodeGenerator
/// This compiler has a compilation stage.
/// </summary>
public bool HasCompilationStage => true;
private static List<Variable> _globalParams = [];

private string GetGlobalParamAndLocalVariableName(CompilationContext context, Variable v)
{
return _globalParams.Contains(v) ? $"({ICodeGenerator.GlobalConfigName}.{v.Name})" : context.Names.GetNameForDecl(v);
}

public void Compile(ICompilerConfiguration job)
{
Expand Down Expand Up @@ -89,6 +95,10 @@ private CompiledFile GenerateSource(CompilationContext context, Scope globalScop

WriteSourcePrologue(context, source.Stream);

_globalParams = globalScope.GetGlobalVariables();

DeclareGlobalParams(context, source.Stream);

// write the top level declarations
foreach (var decl in globalScope.AllDecls)
{
Expand Down Expand Up @@ -242,22 +252,46 @@ private void WriteDecl(CompilationContext context, StringWriter output, IPDecl d
break;

case SafetyTest safety:
WriteSafetyTestDecl(context, output, safety);
ParamAssignment.IterateAssignments(safety, _globalParams,
assignment => WriteSafetyTestDecl(context, output, safety, assignment));
break;

case Interface _:
break;

case EnumElem _:
break;


case Variable _:
break;

default:
declName = context.Names.GetNameForDecl(decl);
context.WriteLine(output, $"// TODO: {decl.GetType().Name} {declName}");
break;
}
}

private void DeclareGlobalParams(CompilationContext context, StringWriter output)
{
WriteNameSpacePrologue(context, output);
context.WriteLine(output, $"public static class {ICodeGenerator.GlobalConfigName}");
context.WriteLine(output, "{");
foreach (var v in _globalParams)
{
if (v.Role != VariableRole.GlobalParams)
{
throw context.Handler.InternalError(v.SourceLocation, new ArgumentException("The role of global variable is not global."));
}
context.Write(output,
$" public static {GetCSharpType(v.Type, true)} {context.Names.GetNameForDecl(v)} = ");
context.Write(output, $"{GetDefaultValue(v.Type)}");
context.WriteLine(output, $";");
}
context.WriteLine(output, "}");
WriteNameSpaceEpilogue(context, output);
}

private void WriteMonitor(CompilationContext context, StringWriter output, Machine machine)
{
WriteNameSpacePrologue(context, output);
Expand Down Expand Up @@ -308,17 +342,19 @@ private static void WriteForeignType(CompilationContext context, StringWriter ou
var declName = foreignType.CanonicalRepresentation;
context.WriteLine(output, $"// TODO: Implement the Foreign Type {declName}");
}

private void WriteSafetyTestDecl(CompilationContext context, StringWriter output, SafetyTest safety)

// For normal test, the assignment is empty dictionary
private void WriteSafetyTestDecl(CompilationContext context, StringWriter output, SafetyTest safety, Dictionary<Variable, IPExpr> assignment)
{
WriteNameSpacePrologue(context, output);

context.WriteLine(output, $"public class {context.Names.GetNameForDecl(safety)} {{");
var name = ParamAssignment.RenameSafetyTestByAssignment(context.Names.GetNameForDecl(safety), assignment);
context.WriteLine(output, $"public class {name} {{");
WriteInitializeGlobalParams(context, output, assignment);
WriteInitializeLinkMap(context, output, safety.ModExpr.ModuleInfo.LinkMap);
WriteInitializeInterfaceDefMap(context, output, safety.ModExpr.ModuleInfo.InterfaceDef);
WriteInitializeMonitorObserves(context, output, safety.ModExpr.ModuleInfo.MonitorMap.Keys);
WriteInitializeMonitorMap(context, output, safety.ModExpr.ModuleInfo.MonitorMap);
WriteTestFunction(context, output, safety.Main);
WriteTestFunction(context, output, safety.Main, true);
context.WriteLine(output, "}");

WriteNameSpaceEpilogue(context, output);
Expand All @@ -333,7 +369,7 @@ private void WriteImplementationDecl(CompilationContext context, StringWriter ou
WriteInitializeInterfaceDefMap(context, output, impl.ModExpr.ModuleInfo.InterfaceDef);
WriteInitializeMonitorObserves(context, output, impl.ModExpr.ModuleInfo.MonitorMap.Keys);
WriteInitializeMonitorMap(context, output, impl.ModExpr.ModuleInfo.MonitorMap);
WriteTestFunction(context, output, impl.Main);
WriteTestFunction(context, output, impl.Main, false);
context.WriteLine(output, "}");

WriteNameSpaceEpilogue(context, output);
Expand Down Expand Up @@ -381,11 +417,27 @@ private void WriteInitializeEnums(CompilationContext context, StringWriter outpu
WriteNameSpaceEpilogue(context, output);
}

private void WriteTestFunction(CompilationContext context, StringWriter output, string main)
private const string InitGlobalParamsFunctionName = "InitializeGlobalParams";

private void WriteInitializeGlobalParams(CompilationContext context, StringWriter output, IDictionary<Variable, IPExpr> dic)
{
context.WriteLine(output, $"public static void {InitGlobalParamsFunctionName}() {{");
foreach (var (v, value) in dic)
{
var varName = GetGlobalParamAndLocalVariableName(context, v);
context.Write(output, $" {varName} = ");
WriteExpr(context, output, value);
context.WriteLine(output, $";");
}
context.WriteLine(output, "}");
}

private void WriteTestFunction(CompilationContext context, StringWriter output, string main, bool ifInitGlobalParams)
{
context.WriteLine(output);
context.WriteLine(output, "[PChecker.SystematicTesting.Test]");
context.WriteLine(output, "public static void Execute(ControlledRuntime runtime) {");
if (ifInitGlobalParams) { context.WriteLine(output, $"{InitGlobalParamsFunctionName}();"); }
context.WriteLine(output, "runtime.RegisterLog(new PCheckerLogTextFormatter());");
context.WriteLine(output, "runtime.RegisterLog(new PCheckerLogJsonFormatter());");
context.WriteLine(output, "PModule.runtime = runtime;");
Expand Down Expand Up @@ -1175,7 +1227,8 @@ private void WriteLValue(CompilationContext context, StringWriter output, IPExpr
break;

case VariableAccessExpr variableAccessExpr:
context.Write(output, context.Names.GetNameForDecl(variableAccessExpr.Variable));
var varName = GetGlobalParamAndLocalVariableName(context, variableAccessExpr.Variable);
context.Write(output, varName);
break;

default:
Expand Down Expand Up @@ -1515,7 +1568,7 @@ private void WriteClone(CompilationContext context, StringWriter output, IExprTe
return;
}

var varName = context.Names.GetNameForDecl(variableRef.Variable);
var varName = GetGlobalParamAndLocalVariableName(context, variableRef.Variable);
context.Write(output, $"(({GetCSharpType(variableRef.Type)})((IPValue){varName})?.Clone())");
}

Expand Down
82 changes: 66 additions & 16 deletions Src/PCompiler/CompilerCore/Backend/PEx/PExCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public IEnumerable<CompiledFile> GenerateCode(ICompilerConfiguration job, Scope
/// This compiler has a compilation stage.
/// </summary>
public bool HasCompilationStage => true;
private static List<Variable> _globalParams = [];

private string GetGlobalParamAndLocalVariableName(Variable v)
{
return _globalParams.Contains(v) ? $"({ICodeGenerator.GlobalConfigName}.{v.Name})" : CompilationContext.GetVar(v.Name);
}

public void Compile(ICompilerConfiguration job)
{
Expand Down Expand Up @@ -103,6 +109,10 @@ private CompiledFile GenerateSource(CompilationContext context, Scope globalScop
context.WriteLine(source.Stream);

context.WriteLine(source.Stream);

_globalParams = globalScope.GetGlobalVariables();

DeclareGlobalParams(context, source.Stream);

//WriteMachineTagDefs(context, source.Stream, globalScope.Machines);

Expand All @@ -118,7 +128,7 @@ private CompiledFile GenerateSource(CompilationContext context, Scope globalScop
WriteDecl(context, source.Stream, decl);
}

if (!hasSafetyTest) WriteMainDriver(context, source.Stream, globalScope, decls);
if (!hasSafetyTest) WriteImplementationDecl(context, source.Stream, globalScope, decls);

context.WriteLine(source.Stream, "PTestDriver testDriver = null;");
context.WriteLine(source.Stream, "@Generated");
Expand All @@ -131,20 +141,36 @@ private CompiledFile GenerateSource(CompilationContext context, Scope globalScop

return source;
}

private const string InitGlobalParamsFunctionName = "InitializeGlobalParams";

private void WriteInitializeGlobalParams(CompilationContext context, StringWriter output, IDictionary<Variable, IPExpr> dic)
{
context.WriteLine(output, $"public static void {InitGlobalParamsFunctionName}() {{");
foreach (var (v, value) in dic)
{
var varName = GetGlobalParamAndLocalVariableName(v);
context.Write(output, $" {varName} = ");
WriteExpr(context, output, value);
context.WriteLine(output, $";");
}
context.WriteLine(output, "}");
}

private void WriteDriver(CompilationContext context, StringWriter output, string startMachine,
IEnumerable<IPDecl> decls, IDictionary<Interface, Machine> interfaceDef = null)
private void WriteTestFunction(CompilationContext context, StringWriter output, string startMachine,
IEnumerable<IPDecl> decls, bool ifInitGlobalParams, IDictionary<Interface, Machine> interfaceDef = null)
{
WriteDriverConfigure(context, output, startMachine, decls, interfaceDef);
WriteTestFunctionConfigure(context, output, startMachine, decls, interfaceDef, ifInitGlobalParams);
context.WriteLine(output);
}

private void WriteDriverConfigure(CompilationContext context, StringWriter output, string startMachine,
IEnumerable<IPDecl> decls, IDictionary<Interface, Machine> interfaceDef)
private void WriteTestFunctionConfigure(CompilationContext context, StringWriter output, string startMachine,
IEnumerable<IPDecl> decls, IDictionary<Interface, Machine> interfaceDef, bool ifInitGlobalParams)
{
context.WriteLine(output);
context.WriteLine(output, "@Generated");
context.WriteLine(output, "public void configure() {");
if (ifInitGlobalParams) { context.WriteLine(output, $" {InitGlobalParamsFunctionName}();"); }

context.WriteLine(output, $" mainMachine = {startMachine}.class;");

Expand Down Expand Up @@ -182,7 +208,7 @@ private void WriteDriverConfigure(CompilationContext context, StringWriter outpu
context.WriteLine(output, "}");
}

private void WriteMainDriver(CompilationContext context, StringWriter output, Scope globalScope,
private void WriteImplementationDecl(CompilationContext context, StringWriter output, Scope globalScope,
IEnumerable<IPDecl> decls)
{
Machine mainMachine = null;
Expand All @@ -204,7 +230,7 @@ private void WriteMainDriver(CompilationContext context, StringWriter output, Sc

context.WriteLine(output, "@Generated");
context.WriteLine(output, "public static class test_DefaultImpl extends PTestDriver {");
WriteDriver(context, output, mainMachine.Name, decls);
WriteTestFunction(context, output, mainMachine.Name, decls, false);
context.WriteLine(output, "}");
context.WriteLine(output);
}
Expand Down Expand Up @@ -235,19 +261,44 @@ private void WriteDecl(CompilationContext context, StringWriter output, IPDecl d
WriteEvent(context, output, ev);
break;
case SafetyTest safety:
WriteSafetyTestDecl(context, output, safety);
ParamAssignment.IterateAssignments(safety, _globalParams,
assignment => WriteSafetyTestDecl(context, output, safety, assignment));
break;
case Variable _:
break;
default:
context.WriteLine(output, $"// Skipping {decl.GetType().Name} '{decl.Name}'\n");
break;
}
}

private void WriteSafetyTestDecl(CompilationContext context, StringWriter output, SafetyTest safety)

private void DeclareGlobalParams(CompilationContext context, StringWriter output)
{
context.WriteLine(output, "@Generated");
context.WriteLine(output, $"public static class {ICodeGenerator.GlobalConfigName}");
context.WriteLine(output, "{");
foreach (var v in _globalParams)
{
if (v.Role != VariableRole.GlobalParams)
{
throw context.Handler.InternalError(v.SourceLocation, new ArgumentException("The role of global variable is not global."));
}
context.Write(output,
$" public static {GetPExType(v.Type)} {v.Name} = ");
context.Write(output, $"{GetDefaultValue(v.Type)}");
context.WriteLine(output, $";");
}
context.WriteLine(output, "}");
context.WriteLine(output);
}

private void WriteSafetyTestDecl(CompilationContext context, StringWriter output, SafetyTest safety, Dictionary<Variable, IPExpr> assignment)
{
context.WriteLine(output, "@Generated");
context.WriteLine(output, $"public static class {context.GetNameForDecl(safety)} extends PTestDriver {{");
WriteDriver(context, output, safety.Main, safety.ModExpr.ModuleInfo.MonitorMap.Keys,
var name = ParamAssignment.RenameSafetyTestByAssignment(context.GetNameForDecl(safety), assignment);
context.WriteLine(output, $"public static class {name} extends PTestDriver {{");
WriteInitializeGlobalParams(context, output, assignment);
WriteTestFunction(context, output, safety.Main, safety.ModExpr.ModuleInfo.MonitorMap.Keys, true,
safety.ModExpr.ModuleInfo.InterfaceDef);
context.WriteLine(output, "}");
context.WriteLine(output);
Expand Down Expand Up @@ -1416,8 +1467,7 @@ private void WriteWithLValueMutationContext(
);
break;
case VariableAccessExpr variableAccessExpr:
var name = variableAccessExpr.Variable.Name;
var unguarded = CompilationContext.GetVar(name);
var unguarded = GetGlobalParamAndLocalVariableName(variableAccessExpr.Variable);
var guardedTemp = context.FreshTempVar();

context.Write(output, $"{GetPExType(variableAccessExpr.Type)} {guardedTemp}");
Expand Down Expand Up @@ -1685,7 +1735,7 @@ private void WriteExpr(CompilationContext context, StringWriter output, IPExpr e
break;
}
case VariableAccessExpr variableAccessExpr:
context.Write(output, $"{CompilationContext.GetVar(variableAccessExpr.Variable.Name)}");
context.Write(output, $"{GetGlobalParamAndLocalVariableName(variableAccessExpr.Variable)}");
break;
case FunCallExpr _:
throw new InvalidOperationException(
Expand Down
25 changes: 25 additions & 0 deletions Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Linq;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using Plang.Compiler.TypeChecker;
using Plang.Compiler.TypeChecker.AST;
using Plang.Compiler.TypeChecker.AST.Declarations;
Expand Down Expand Up @@ -52,6 +53,30 @@ public Exception DuplicateDeclaration(ParserRuleContext location, IPDecl duplica
return IssueError(location,
$"'{duplicate.Name}' duplicates declaration '{existing.Name}' at {locationResolver.GetLocation(existing.SourceLocation)}");
}

public Exception RedeclareGlobalParam(ParserRuleContext location, IPDecl duplicate, IPDecl existing)
{
return IssueError(location,
$"'{duplicate.Name}' redeclares a global param '{existing.Name}' at {locationResolver.GetLocation(existing.SourceLocation)}");
}

public Exception UndeclaredGlobalParam(ParserRuleContext location, string name)
{
return IssueError(location,
$"'global param {name}' is not undeclared");
}

public Exception ModifyGlobalParam(ParserRuleContext location, IPDecl existing)
{
return IssueError(location,
$"try to modify a global param '{existing.Name}' at {locationResolver.GetLocation(existing.SourceLocation)}");
}

public Exception InvalidTwise(ParserRuleContext location, IPDecl testDecl, string errMsg)
{
return IssueError(location,
$"invalid twise number at {locationResolver.GetLocation(testDecl.SourceLocation)}: {errMsg}");
}

public Exception IncorrectArgumentCount(ParserRuleContext location, int actualCount, int expectedCount)
{
Expand Down
9 changes: 9 additions & 0 deletions Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using Plang.Compiler.TypeChecker.AST;
using Plang.Compiler.TypeChecker.AST.Declarations;
using Plang.Compiler.TypeChecker.AST.States;
Expand All @@ -23,6 +24,14 @@ Exception DuplicateStartState(
Exception DuplicateStateEntry(ParserRuleContext location, Function existingHandler, State state);

Exception DuplicateDeclaration(ParserRuleContext location, IPDecl duplicate, IPDecl existing);

Exception RedeclareGlobalParam(ParserRuleContext location, IPDecl duplicate, IPDecl existing);

Exception UndeclaredGlobalParam(ParserRuleContext location, string name);

Exception ModifyGlobalParam(ParserRuleContext location, IPDecl existing);

Exception InvalidTwise(ParserRuleContext location, IPDecl testDecl, string errMsg);

Exception IncorrectArgumentCount(ParserRuleContext location, int actualCount, int expectedCount);

Expand Down
Loading
Loading