Beispiel #1
0
        public void Extract(ModuleDefinition m, ArmadaSymbolTable symbols, List <MemberDecl> newDefaultClassDecls,
                            List <DatatypeCtor> entryCtors, List <MatchCaseExpr> overallNextCases,
                            List <MatchCaseExpr> validStepCases, List <MatchCaseExpr> crashAvoidanceCases,
                            List <MatchCaseExpr> getNextStateCases)
        {
            if (!valid)
            {
                return;
            }

            var entryFormals = new List <Formal> {
                AH.MakeFormal("tid", "Armada_ThreadHandle")
            };
            var commonFormals = new List <Formal> {
                AH.MakeFormal("s", "Armada_TotalState"),
                AH.MakeFormal("tid", "Armada_ThreadHandle")
            };
            var overallNextFormals = new List <Formal> {
                AH.MakeFormal("s", "Armada_TotalState"),
                AH.MakeFormal("s'", "Armada_TotalState"),
                AH.MakeFormal("tid", "Armada_ThreadHandle")
            };
            var caseArguments = new List <BoundVar> {
                AH.MakeBoundVar("tid", "Armada_ThreadHandle")
            };
            var commonMatchBodyArguments = new List <Expression>()
            {
                AH.MakeNameSegment("s", "Armada_TotalState"),
                AH.MakeNameSegment("tid", "Armada_ThreadHandle")
            };
            var overallNextMatchBodyArguments = new List <Expression>()
            {
                AH.MakeNameSegment("s", "Armada_TotalState"),
                AH.MakeNameSegment("s'", "Armada_TotalState"),
                AH.MakeNameSegment("tid", "Armada_ThreadHandle")
            };

            foreach (var f in formals)
            {
                var flattened_type = symbols.FlattenType(f.VarType);
                commonFormals.Add(AH.MakeFormal(f.LocalVarName, flattened_type));
                overallNextFormals.Add(AH.MakeFormal(f.LocalVarName, flattened_type));
                entryFormals.Add(AH.MakeFormal(f.GloballyUniqueVarName, flattened_type));
                caseArguments.Add(AH.MakeBoundVar(f.LocalVarName, flattened_type));
                commonMatchBodyArguments.Add(AH.MakeNameSegment(f.LocalVarName, flattened_type));
                overallNextMatchBodyArguments.Add(AH.MakeNameSegment(f.LocalVarName, flattened_type));
            }

            var nameSuffix = NameSuffix;
            var entryName  = $"Armada_TraceEntry_{nameSuffix}";

            entryCtors.Add(AH.MakeDatatypeCtor(entryName, entryFormals));

            var validStepFn        = AH.MakeNameSegment($"Armada_ValidStep_{nameSuffix}", (Type)null);
            var validStepMatchBody = AH.SetExprType(new ApplySuffix(Token.NoToken, validStepFn, commonMatchBodyArguments), new BoolType());

            validStepCases.Add(AH.MakeMatchCaseExpr(entryName, caseArguments, validStepMatchBody));
            var validStepPredBody = validStepBuilder.Extract();
            var validStepName     = $"Armada_ValidStep_{nameSuffix}";
            var validStepPred     = AH.MakePredicate(validStepName, commonFormals, validStepPredBody);

            newDefaultClassDecls.Add(validStepPred);

            var crashAvoidanceFn        = AH.MakeNameSegment($"Armada_UndefinedBehaviorAvoidance_{nameSuffix}", (Type)null);
            var crashAvoidanceMatchBody = AH.SetExprType(new ApplySuffix(Token.NoToken, crashAvoidanceFn, commonMatchBodyArguments), new BoolType());

            crashAvoidanceCases.Add(AH.MakeMatchCaseExpr(entryName, caseArguments, crashAvoidanceMatchBody));
            var crashAvoidanceName     = $"Armada_UndefinedBehaviorAvoidance_{nameSuffix}";
            var crashAvoidancePredBody = crashAvoidanceBuilder.Extract();
            var crashAvoidancePred     = AH.MakePredicateWithReq(crashAvoidanceName, commonFormals, validStepMatchBody, crashAvoidancePredBody);

            newDefaultClassDecls.Add(crashAvoidancePred);

            var getNextStateFn        = AH.MakeNameSegment($"Armada_GetNextState_{nameSuffix}", (Type)null);
            var getNextStateMatchBody = AH.SetExprType(new ApplySuffix(Token.NoToken, getNextStateFn, commonMatchBodyArguments), "Armada_TotalState");

            getNextStateCases.Add(AH.MakeMatchCaseExpr(entryName, caseArguments, getNextStateMatchBody));
            var getNextStateName   = $"Armada_GetNextState_{nameSuffix}";
            var getNextStateFnBody = getNextStateBuilder.Extract();
            var getNextStateReq    = AH.MakeAndExpr(validStepMatchBody, crashAvoidanceMatchBody);
            var getNextStateFunc   = AH.MakeFunctionWithReq(getNextStateName, commonFormals, getNextStateReq, getNextStateFnBody);

            newDefaultClassDecls.Add(getNextStateFunc);

            // predicate Armada_Next_{nameSuffix}(s:Armada_TotalState, s':Armada_TotalState, ...) {
            //     && Armada_ValidStep_{nameSuffix}(s, ...)
            //     && s' == if Armada_UndefinedBehaviorAvoidance(s, ...) then Armada_GetNextState(s, ...)
            //              else s.(stop_reason := Armada_StopReasonUndefinedBehavior)
            // }

            var s_with_undefined_behavior =
                AH.MakeDatatypeUpdateExpr(s, "stop_reason", AH.MakeNameSegment("Armada_StopReasonUndefinedBehavior", "Armada_StopReason"));
            var target_s_prime  = AH.MakeIfExpr(crashAvoidanceMatchBody, getNextStateMatchBody, s_with_undefined_behavior);
            var s_prime         = AH.MakeNameSegment("s'", "Armada_TotalState");
            var s_prime_correct = AH.MakeEqExpr(s_prime, target_s_prime);
            var overallNextBody = AH.MakeAndExpr(validStepMatchBody, s_prime_correct);
            var overallNextName = $"Armada_Next_{nameSuffix}";
            var overallNextPred = AH.MakePredicate(overallNextName, overallNextFormals, overallNextBody);

            newDefaultClassDecls.Add(overallNextPred);

            var overallNextFn        = AH.MakeNameSegment(overallNextName, (Type)null);
            var overallNextMatchBody = AH.SetExprType(new ApplySuffix(Token.NoToken, overallNextFn, overallNextMatchBodyArguments),
                                                      new BoolType());

            overallNextCases.Add(AH.MakeMatchCaseExpr(entryName, caseArguments, overallNextMatchBody));
        }