Skip to content

Commit

Permalink
[PExplicit] Several minor updates
Browse files Browse the repository at this point in the history
Support goto event handlers with transition functions

Correct PTuple constructor

Correct status and result when a bug is bound

Cleanup return in functions in IR.

Start adding pending/unsupported tests in PExplicit test runner
  • Loading branch information
aman-goel committed Apr 9, 2024
1 parent 22053d3 commit 1596b74
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 128 deletions.
166 changes: 39 additions & 127 deletions Src/PCompiler/CompilerCore/Backend/PExplicit/PExplicitCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ private void WriteMachineConstructor(CompilationContext context, StringWriter ou

private void WriteHandlerUpdate(CompilationContext context, StringWriter output, State state)
{
context.Write(output, $"{context.GetNameForDecl(state)}.registerHandlers(");
context.WriteLine(output, $"{context.GetNameForDecl(state)}.registerHandlers(");
var first = true;
foreach (var handler in state.AllEventHandlers)
{
Expand Down Expand Up @@ -536,7 +536,8 @@ private void WriteEventHandler(CompilationContext context, StringWriter output,
if (transitionFunc.Signature.Parameters.Count() == 1)
{
Debug.Assert(!transitionFunc.Signature.Parameters[0].Type.IsSameTypeAs(PrimitiveType.Null));
context.Write(output, $", payload");
var payloadVsType = GetPExplicitType(transitionFunc.Signature.Parameters[0].Type);
context.Write(output, $", ({payloadVsType}) payload");
}
context.WriteLine(output, ");");
context.WriteLine(output, "}");
Expand Down Expand Up @@ -573,35 +574,6 @@ internal ControlFlowContext FreshBranchSubContext(CompilationContext context)
}
}

private bool MayExitWithOutcome(Function func)
{
return (func.CanChangeState ?? false) || (func.CanRaiseEvent ?? false);
}

private enum FunctionReturnConvention
{
ReturnValue,
ReturnVoid,
ReturnValueOrExit
}

private FunctionReturnConvention GetReturnConvention(Function function)
{
var mayExit = MayExitWithOutcome(function);
var voidReturn = function.Signature.ReturnType.IsSameTypeAs(PrimitiveType.Null);
if (voidReturn)
{
return FunctionReturnConvention.ReturnVoid;
}
else
{
if (mayExit)
return FunctionReturnConvention.ReturnValueOrExit;
else
return FunctionReturnConvention.ReturnValue;
}
}

private void WriteForeignFunction(CompilationContext context, StringWriter output, Function function)
{
var isStatic = function.Owner == null;
Expand All @@ -613,15 +585,12 @@ private void WriteForeignFunction(CompilationContext context, StringWriter outpu

string returnType = null;
var returnStatement = "";
switch (GetReturnConvention(function))
{
case FunctionReturnConvention.ReturnValue:
returnType = "Object";
returnStatement = "return ";
break;
case FunctionReturnConvention.ReturnVoid:
returnType = "void";
break;
if (function.Signature.ReturnType.IsSameTypeAs(PrimitiveType.Null)) {
returnType = "void";

} else {
returnType = "Object";
returnStatement = "return ";
}

var functionName = $"wrapper__{context.GetNameForDecl(function)}";
Expand Down Expand Up @@ -660,17 +629,10 @@ private void WriteFunction(CompilationContext context, StringWriter output, Func
var staticKeyword = isStatic ? "static " : "";

string returnType = null;
switch (GetReturnConvention(function))
{
case FunctionReturnConvention.ReturnValue:
returnType = GetPExplicitType(function.Signature.ReturnType);
break;
case FunctionReturnConvention.ReturnVoid:
returnType = "void";
break;
case FunctionReturnConvention.ReturnValueOrExit:
returnType = GetPExplicitType(function.Signature.ReturnType);
break;
if (function.Signature.ReturnType.IsSameTypeAs(PrimitiveType.Null)) {
returnType = "void";
} else {
returnType = GetPExplicitType(function.Signature.ReturnType);
}

context.ReturnType = function.Signature.ReturnType;
Expand Down Expand Up @@ -708,15 +670,8 @@ private void WriteFunctionBody(CompilationContext context, StringWriter output,
context.WriteLine(output);
}

var returnConvention = GetReturnConvention(function);
switch (returnConvention)
{
case FunctionReturnConvention.ReturnValue:
case FunctionReturnConvention.ReturnValueOrExit:
context.WriteLine(output, $"{GetPExplicitType(function.Signature.ReturnType)} {CompilationContext.ReturnValue} = new {GetPExplicitType(function.Signature.ReturnType)}({GetDefaultValue(function.Signature.ReturnType)});");
break;
case FunctionReturnConvention.ReturnVoid:
break;
if (!function.Signature.ReturnType.IsSameTypeAs(PrimitiveType.Null)) {
context.WriteLine(output, $"{GetPExplicitType(function.Signature.ReturnType)} {CompilationContext.ReturnValue} = new {GetPExplicitType(function.Signature.ReturnType)}({GetDefaultValue(function.Signature.ReturnType)});");
}

if (function is WhileFunction)
Expand All @@ -727,18 +682,6 @@ private void WriteFunctionBody(CompilationContext context, StringWriter output,
{
WriteStmt(function, context, output, ControlFlowContext.FreshFuncContext(context), function.Body);
}

switch (returnConvention)
{
case FunctionReturnConvention.ReturnValue:
context.WriteLine(output, $"return {CompilationContext.ReturnValue};");
break;
case FunctionReturnConvention.ReturnVoid:
break;
case FunctionReturnConvention.ReturnValueOrExit:
context.WriteLine(output, $"return {CompilationContext.ReturnValue};");
break;
}
}

private FunCallExpr TryGetCallInAssignment(IPStmt stmt)
Expand Down Expand Up @@ -1315,35 +1258,22 @@ private string GetInlineCastPrefix(PLanguageType valueType, PLanguageType locati

private void WriteForeignFunCallStmt(CompilationContext context, StringWriter output, Function function, IReadOnlyList<IPExpr> args, IPExpr dest=null)
{
var returnConvention = GetReturnConvention(function);
string returnTemp = null;
switch (returnConvention)
{
case FunctionReturnConvention.ReturnValue:
returnTemp = context.FreshTempVar();
context.Write(output, $"{GetPExplicitType(function.Signature.ReturnType)} {returnTemp} = ({GetPExplicitType(function.Signature.ReturnType)})");
break;
case FunctionReturnConvention.ReturnVoid:
break;
default:
throw new NotImplementedException("Cannot handle foreign function calls that can exit");
if (!function.Signature.ReturnType.IsSameTypeAs(PrimitiveType.Null)) {
returnTemp = context.FreshTempVar();
context.Write(output, $"{GetPExplicitType(function.Signature.ReturnType)} {returnTemp} = ({GetPExplicitType(function.Signature.ReturnType)})");
}

context.Write(output, $"ForeignFunctionInvoker.invoke(");
switch (returnConvention)
{
case FunctionReturnConvention.ReturnValue:
context.Write(output, $"{GetDefaultValue(function.Signature.ReturnType)}, x -> ");
context.Write(output, "{ return ");
context.Write(output, $"wrapper__{context.GetNameForDecl(function)}(x);");
context.Write(output, " }");
break;
case FunctionReturnConvention.ReturnVoid:
context.Write(output, $"x -> wrapper__{context.GetNameForDecl(function)}(x)");
break;
default:
throw new NotImplementedException("Cannot handle foreign function calls that can exit");
if (function.Signature.ReturnType.IsSameTypeAs(PrimitiveType.Null)) {
context.Write(output, $"x -> wrapper__{context.GetNameForDecl(function)}(x)");
} else {
context.Write(output, $"{GetDefaultValue(function.Signature.ReturnType)}, x -> ");
context.Write(output, "{ return ");
context.Write(output, $"wrapper__{context.GetNameForDecl(function)}(x);");
context.Write(output, " }");
}

for (var i = 0; i < args.Count(); i++)
{
var param = args.ElementAt(i);
Expand All @@ -1352,17 +1282,11 @@ private void WriteForeignFunCallStmt(CompilationContext context, StringWriter ou
}
context.WriteLine(output, ");");

switch (returnConvention)
{
case FunctionReturnConvention.ReturnValue:
if (dest != null)
WriteWithLValueMutationContext(context, output, dest, false, (lhs) => context.WriteLine(output, $"{lhs} = {returnTemp};"));
break;
case FunctionReturnConvention.ReturnVoid:
Debug.Assert(dest == null);
break;
default:
throw new NotImplementedException("Cannot handle foreign function calls that can exit");
if (function.Signature.ReturnType.IsSameTypeAs(PrimitiveType.Null)) {
Debug.Assert(dest == null);
} else {
if (dest != null)
WriteWithLValueMutationContext(context, output, dest, false, (lhs) => context.WriteLine(output, $"{lhs} = {returnTemp};"));
}
}

Expand All @@ -1374,17 +1298,10 @@ private void WriteFunCallStmt(CompilationContext context, StringWriter output, F
return;
}

var returnConvention = GetReturnConvention(function);
string returnTemp = null;
switch (returnConvention)
{
case FunctionReturnConvention.ReturnValue:
case FunctionReturnConvention.ReturnValueOrExit:
returnTemp = context.FreshTempVar();
context.Write(output, $"{GetPExplicitType(function.Signature.ReturnType)} {returnTemp} = ");
break;
case FunctionReturnConvention.ReturnVoid:
break;
if (!function.Signature.ReturnType.IsSameTypeAs(PrimitiveType.Null)) {
returnTemp = context.FreshTempVar();
context.Write(output, $"{GetPExplicitType(function.Signature.ReturnType)} {returnTemp} = ");
}

context.Write(output, $"{context.GetNameForDecl(function)}({CompilationContext.CurrentMachine}");
Expand All @@ -1399,16 +1316,11 @@ private void WriteFunCallStmt(CompilationContext context, StringWriter output, F

context.WriteLine(output, ");");

switch (returnConvention)
{
case FunctionReturnConvention.ReturnValue:
case FunctionReturnConvention.ReturnValueOrExit:
if (dest != null)
WriteWithLValueMutationContext(context, output, dest, false, (lhs) => context.WriteLine(output, $"{lhs} = {returnTemp};"));
break;
case FunctionReturnConvention.ReturnVoid:
Debug.Assert(dest == null);
break;
if (function.Signature.ReturnType.IsSameTypeAs(PrimitiveType.Null)) {
Debug.Assert(dest == null);
} else {
if (dest != null)
WriteWithLValueMutationContext(context, output, dest, false, (lhs) => context.WriteLine(output, $"{lhs} = {returnTemp};"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private static void process(boolean resume) throws Exception {
if (PExplicitGlobal.getConfig().getVerbosity() > 0) {
PExplicitLogger.printStackTrace(e, false);
}
throw e;
} catch (InterruptedException e) {
PExplicitGlobal.setStatus("interrupted");
throw new Exception("INTERRUPTED", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public GotoEventHandler(PEvent event, State dest) {
this.gotoState = dest;
}

public void transitionFunction(PMachine target, PValue<?> payload) {
}

/**
* Handle the goto event at the target machine.
*
Expand All @@ -31,6 +34,7 @@ public GotoEventHandler(PEvent event, State dest) {
*/
@Override
public void handleEvent(PMachine target, PValue<?> payload) {
transitionFunction(target, payload);
target.processStateTransition(gotoState, payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class PTuple extends PValue<PTuple> {
/**
* Creates a new PTuple with the given fields
*/
public PTuple(PValue<?>[] input_fields) {
public PTuple(PValue<?> ... input_fields) {
this.fields = new PValue<?>[input_fields.length];
for (int i = 0; i < input_fields.length; i++) {
this.fields[i] = PValue.clone(input_fields[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ private static void setRunArgs() {
}

private static void createExcludeList() {
/**
* TODO: Support type casting
*/
// TODO: Cannot yet handle casting to variable of type (int,int) from value of type any
excluded.add("../../../Tst/RegressionTests/Feature1SMLevelDecls/Correct/bug3");
excluded.add("../../../Tst/RegressionTests/Feature1SMLevelDecls/Correct/bug4");
// TODO: Cannot yet handle casting to variable of type (machine,machine) from value of type any
excluded.add("../../../Tst/RegressionTests/Feature1SMLevelDecls/Correct/PingPong");

/**
* TODO: Null events
*/
// TODO: Null actions are not supported
excluded.add("../../../Tst/RegressionTests/Feature1SMLevelDecls/Correct/BugRepro");
excluded.add("../../../Tst/RegressionTests/Feature1SMLevelDecls/Correct/MoreThan32Events");



}

private static void initialize() {
Expand Down

0 comments on commit 1596b74

Please sign in to comment.