diff --git a/Src/PCompiler/CompilerCore/Backend/PExplicit/PExplicitCodeGenerator.cs b/Src/PCompiler/CompilerCore/Backend/PExplicit/PExplicitCodeGenerator.cs index 7aa4d04d5..42bd6c640 100644 --- a/Src/PCompiler/CompilerCore/Backend/PExplicit/PExplicitCodeGenerator.cs +++ b/Src/PCompiler/CompilerCore/Backend/PExplicit/PExplicitCodeGenerator.cs @@ -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) { @@ -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, "}"); @@ -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; @@ -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)}"; @@ -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; @@ -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) @@ -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) @@ -1315,35 +1258,22 @@ private string GetInlineCastPrefix(PLanguageType valueType, PLanguageType locati private void WriteForeignFunCallStmt(CompilationContext context, StringWriter output, Function function, IReadOnlyList 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); @@ -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};")); } } @@ -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}"); @@ -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};")); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java index c544bdfb3..ee96ca4db 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/RuntimeExecutor.java @@ -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); diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/eventhandlers/GotoEventHandler.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/eventhandlers/GotoEventHandler.java index f5cdda055..7abf0fcee 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/eventhandlers/GotoEventHandler.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/runtime/machine/eventhandlers/GotoEventHandler.java @@ -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. * @@ -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); } } diff --git a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/values/PTuple.java b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/values/PTuple.java index 68e39d119..a6b6e5d52 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/values/PTuple.java +++ b/Src/PRuntimes/PExplicitRuntime/src/main/java/pexplicit/values/PTuple.java @@ -13,7 +13,7 @@ public class PTuple extends PValue { /** * 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]); diff --git a/Src/PRuntimes/PExplicitRuntime/src/test/java/pexplicit/TestPExplicit.java b/Src/PRuntimes/PExplicitRuntime/src/test/java/pexplicit/TestPExplicit.java index cade71949..5c406dde4 100644 --- a/Src/PRuntimes/PExplicitRuntime/src/test/java/pexplicit/TestPExplicit.java +++ b/Src/PRuntimes/PExplicitRuntime/src/test/java/pexplicit/TestPExplicit.java @@ -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() {