Example #1
0
            private ExpressionSyntax ConstructType(FuncTerm type)
            {
                string           typeKind = ((Id)type.Function).Name;
                ExpressionSyntax typeExpr;
                string           typeName;

                if (ImportedTypes.ContainsKey(Factory.Instance.ToAST(type)))
                {
                    typeName = GetNextTypeName(ImportedTypes[Factory.Instance.ToAST(type)]);
                    return(GetTypeExpr(typeName));
                }

                var originalName = "Interface";

                if (ExportedTypes.ContainsKey(Factory.Instance.ToAST(type)))
                {
                    originalName = ExportedTypes[Factory.Instance.ToAST(type)];
                    typeName     = GetNextTypeName(ExportedTypes[Factory.Instance.ToAST(type)]);
                    typeExpr     = GetTypeExpr(typeName);
                }
                else
                {
                    typeName = GetNextTypeName();
                    typeExpr = GetTypeExpr(typeName);
                }

                // add declaration and initialization
                if (typeKind == "BaseType")
                {
                    string primitiveType = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(((Id)GetArgByIndex(type, 0)).Name.ToLowerInvariant());

                    Debug.Assert(
                        primitiveType == "Any" || primitiveType == "Null" || primitiveType == "Bool" || primitiveType == "Int" ||
                        primitiveType == "Event" || primitiveType == "Machine",
                        $"Illegal BaseType: {primitiveType}");

                    if (primitiveType != "Any")
                    {
                        AddTypeInitialization(
                            typeExpr,
                            CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName($"Prt{primitiveType}Type")));
                        AddTypeDeclaration(typeName);
                    }
                }
                else if (typeKind == "AnyType")
                {
                    AddTypeDeclaration(typeName);
                    AddTypeInitialization(
                        typeExpr,
                        CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtAnyType")));
                }
                else if (typeKind == "NameType")
                {
                    string enumTypeName = (GetArgByIndex(type, 0) as Cnst).GetStringValue();
                    var    args         = new List <ExpressionSyntax> {
                        CSharpHelper.MkCSharpStringLiteralExpression(enumTypeName)
                    };
                    foreach (KeyValuePair <string, int> x in pToCSharp.allEnums[enumTypeName])
                    {
                        args.Add(CSharpHelper.MkCSharpStringLiteralExpression(x.Key));
                        args.Add(CSharpHelper.MkCSharpNumericLiteralExpression(x.Value));
                    }

                    AddTypeDeclaration(typeName);
                    AddTypeInitialization(
                        typeExpr,
                        CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtEnumType"), args.ToArray()));
                }
                else if (typeKind == "TupType")
                {
                    var memberTypes = new List <SyntaxNodeOrToken>();
                    while (type != null)
                    {
                        memberTypes.Add(PTypeToCSharpExpr((FuncTerm)GetArgByIndex(type, 0)));
                        type = GetArgByIndex(type, 1) as FuncTerm;
                    }

                    var initializer = CSharpHelper.Intersperse(memberTypes, SyntaxFactory.Token(SyntaxKind.CommaToken));

                    AddTypeDeclaration(typeName);
                    AddTypeInitialization(
                        typeExpr,
                        CSharpHelper.MkCSharpObjectCreationExpression(
                            SyntaxFactory.IdentifierName("PrtTupleType"),
                            CSharpHelper.MkCSharpArrayCreationExpression("PrtType", initializer.ToArray())));
                }
                else if (typeKind == "NmdTupType")
                {
                    var memberNames = new List <SyntaxNode>();
                    var memberTypes = new List <SyntaxNode>();

                    while (type != null)
                    {
                        var    typeField = (FuncTerm)GetArgByIndex(type, 0);
                        string nameField = ((Cnst)GetArgByIndex(typeField, 0)).GetStringValue();
                        memberNames.Add(CSharpHelper.MkCSharpStringLiteralExpression(nameField));
                        memberTypes.Add(PTypeToCSharpExpr((FuncTerm)GetArgByIndex(typeField, 1)));
                        type = GetArgByIndex(type, 1) as FuncTerm;
                    }

                    var initializer = new List <SyntaxNodeOrToken>();
                    var ind         = 0;
                    foreach (SyntaxNode memberName in memberNames)
                    {
                        initializer.Add(memberName);
                        initializer.Add(SyntaxFactory.Token(SyntaxKind.CommaToken));
                        initializer.Add(memberTypes[ind++]);
                        initializer.Add(SyntaxFactory.Token(SyntaxKind.CommaToken));
                    }

                    initializer.RemoveAt(initializer.Count - 1);
                    AddTypeDeclaration(typeName);
                    AddTypeInitialization(
                        typeExpr,
                        CSharpHelper.MkCSharpObjectCreationExpression(
                            SyntaxFactory.IdentifierName("PrtNamedTupleType"),
                            CSharpHelper.MkCSharpArrayCreationExpression("object", initializer.ToArray())));
                }
                else if (typeKind == "SeqType")
                {
                    SyntaxNode innerType = PTypeToCSharpExpr((FuncTerm)GetArgByIndex(type, 0));
                    AddTypeDeclaration(typeName);
                    AddTypeInitialization(
                        typeExpr,
                        CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtSeqType"), innerType));
                }
                else if (typeKind == "MapType")
                {
                    SyntaxNode keyType = PTypeToCSharpExpr((FuncTerm)GetArgByIndex(type, 0));
                    SyntaxNode valType = PTypeToCSharpExpr((FuncTerm)GetArgByIndex(type, 1));
                    AddTypeDeclaration(typeName);
                    AddTypeInitialization(
                        typeExpr,
                        CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtMapType"), keyType, valType));
                }
                else
                {
                    // typekind == "InterfaceType"
                    ObjectCreationExpressionSyntax initializer = CSharpHelper.MkCSharpObjectCreationExpression(
                        SyntaxFactory.IdentifierName("PrtInterfaceType"),
                        CSharpHelper.MkCSharpStringLiteralExpression(originalName));
                    AddTypeDeclaration(typeName);
                    AddTypeInitialization(typeExpr, initializer);
                }

                return(typeExpr);
            }
Example #2
0
        // if isAnonymous is true, parameters is actually envVars
        public FunInfo(bool isAnonymous, FuncTerm parameters, AST <FuncTerm> returnType, FuncTerm locals, Node body)
        {
            this.isAnonymous = isAnonymous;
            this.returnType  = returnType;
            this.body        = body;

            this.parameterNames      = new List <string>();
            this.localNameToInfo     = new Dictionary <string, LocalVariableInfo>();
            this.localNames          = new List <string>();
            this.numFairChoices      = 0;
            this.typeInfo            = new Dictionary <AST <Node>, FuncTerm>();
            this.maxNumLocals        = 0;
            this.invokeSchedulerFuns = new HashSet <Node>();
            this.invokePluginFuns    = new HashSet <Node>();
            this.printArgs           = new HashSet <string>();

            int paramIndex = 0;

            while (parameters != null)
            {
                var ft = (FuncTerm)PTranslation.GetArgByIndex(parameters, 0);
                using (var enumerator = ft.Args.GetEnumerator())
                {
                    enumerator.MoveNext();
                    var varName = ((Cnst)enumerator.Current).GetStringValue();
                    enumerator.MoveNext();
                    var varType = (FuncTerm)enumerator.Current;
                    localNameToInfo[varName] = new LocalVariableInfo(varType, paramIndex);
                    parameterNames.Add(varName);
                }
                parameters = PTranslation.GetArgByIndex(parameters, 1) as FuncTerm;
                paramIndex++;
            }

            int localIndex = paramIndex;

            while (locals != null)
            {
                var ft = (FuncTerm)PToZing.GetArgByIndex(locals, 0);
                using (var enumerator = ft.Args.GetEnumerator())
                {
                    enumerator.MoveNext();
                    var varName = ((Cnst)enumerator.Current).GetStringValue();
                    enumerator.MoveNext();
                    var varType = (FuncTerm)enumerator.Current;
                    localNameToInfo[varName] = new LocalVariableInfo(varType, localIndex);
                    localNames.Add(varName);
                }
                locals = PToZing.GetArgByIndex(locals, 1) as FuncTerm;
                localIndex++;
            }
        }
Example #3
0
 public EventInfo(FuncTerm payloadType)
 {
     this.payloadType  = payloadType;
     this.maxInstances = -1;
 }
Example #4
0
 public EventInfo(int maxInstances, bool maxInstancesAssumed, FuncTerm payloadType)
 {
     this.maxInstances        = maxInstances;
     this.maxInstancesAssumed = maxInstancesAssumed;
     this.payloadType         = payloadType;
 }
Example #5
0
 public LocalVariableInfo(FuncTerm type, int index) : base(type)
 {
     this.index = index;
 }
Example #6
0
        private void GenerateTypeInfo()
        {
            var terms = GetBin(factBins, "TypeOf");

            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var      typingContextAlias = Factory.Instance.ToAST(it.Current);
                    FuncTerm typingContext      = aliasToTerm[typingContextAlias];
                    it.MoveNext();
                    var expr = Factory.Instance.ToAST(it.Current);
                    it.MoveNext();
                    var type = it.Current as FuncTerm;

                    string typingContextKind = ((Id)typingContext.Function).Name;
                    if (typingContextKind == "FunDecl")
                    {
                        string ownerName = GetOwnerName(typingContext, 1);
                        string funName   = GetName(typingContext, 0);
                        if (ownerName == null)
                        {
                            allGlobalFuns[funName].typeInfo[expr] = type;
                        }
                        else
                        {
                            allMachines[ownerName].funNameToFunInfo[funName].typeInfo[expr] = type;
                        }
                    }
                    else
                    {
                        // typingContextKind == "AnonFunDecl"
                        string ownerName = GetOwnerName(typingContext, 0);
                        string funName   = anonFunToName[typingContextAlias];
                        if (ownerName == null)
                        {
                            allGlobalFuns[funName].typeInfo[expr] = type;
                        }
                        else
                        {
                            allMachines[ownerName].funNameToFunInfo[funName].typeInfo[expr] = type;
                        }
                    }
                }
            }

            terms = GetBin(factBins, "ImportedType");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var typeName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    //ignore the duplicate imported types
                    if (!typeContext.ImportedTypes.ContainsKey(Factory.Instance.ToAST(it.Current)))
                    {
                        typeContext.ImportedTypes.Add(Factory.Instance.ToAST(it.Current), typeName);
                        typeContext.PTypeToCSharpExpr((FuncTerm)it.Current);
                    }
                }
            }

            terms = GetBin(factBins, "ExportedType");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var typeName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    //if duplicate exported type then add it to duplicate and declare it separately in MkType()
                    if (typeContext.ExportedTypes.ContainsKey(Factory.Instance.ToAST(it.Current)))
                    {
                        typeContext.DuplicateExportedTypes.Add(typeName, it.Current as FuncTerm);
                    }
                    else
                    {
                        typeContext.ExportedTypes.Add(Factory.Instance.ToAST(it.Current), typeName);
                        typeContext.PTypeToCSharpExpr((FuncTerm)it.Current);
                    }
                }
            }

            terms = GetBin(factBins, "TranslatedTypeExpr");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var eType = (FuncTerm)it.Current;
                    typeContext.PTypeToCSharpExpr(eType);
                }
            }

            terms = GetBin(factBins, "TypeExpansion");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var type = (FuncTerm)it.Current;
                    it.MoveNext();
                    var eType = (FuncTerm)it.Current;
                    typeContext.AddOriginalType(type, eType);
                }
            }
        }
Example #7
0
        public LinkedList <AST <FuncTerm> > GetBin(Dictionary <string, LinkedList <AST <FuncTerm> > > factBins, FuncTerm ft)
        {
            var fun = (Id)ft.Function;

            return(GetBin(factBins, fun.Name));
        }
Example #8
0
        void GenerateLinkerInfo(AST <Program> model)
        {
            var linkerModel = model.FindAny(
                new NodePred[] { NodePredFactory.Instance.MkPredicate(NodeKind.Program), NodePredFactory.Instance.MkPredicate(NodeKind.Model) });

            var factBins = new Dictionary <string, LinkedList <AST <FuncTerm> > >();

            linkerModel.FindAll(
                new NodePred[]
            {
                NodePredFactory.Instance.Star,
                NodePredFactory.Instance.MkPredicate(NodeKind.ModelFact)
            },

                (path, n) =>
            {
                var mf      = (ModelFact)n;
                FuncTerm ft = (FuncTerm)mf.Match;
                GetBin(factBins, ft).AddLast((AST <FuncTerm>)Factory.Instance.ToAST(ft));
            });
            LinkedList <AST <FuncTerm> > terms;


            terms = GetBin(factBins, "CSharpLinkMap");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var currMachineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var IorMName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var impMachineName = ((Cnst)it.Current).GetStringValue();

                    if (allTests.ContainsKey(name))
                    {
                        if (allTests[name].linkMap.ContainsKey(currMachineName))
                        {
                            allTests[name].linkMap[currMachineName].Add(IorMName, impMachineName);
                        }
                        else
                        {
                            allTests[name].linkMap[currMachineName] = new Dictionary <string, string>();
                            allTests[name].linkMap[currMachineName].Add(IorMName, impMachineName);
                        }
                    }
                    else
                    {
                        allTests[name] = new TestCaseInfo();
                        allTests[name].linkMap[currMachineName] = new Dictionary <string, string>();
                        allTests[name].linkMap[currMachineName].Add(IorMName, impMachineName);
                    }
                }
            }

            terms = GetBin(factBins, "CSharpRenameMap");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var renamedMachineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var impName = ((Cnst)it.Current).GetStringValue();

                    if (allTests.ContainsKey(name))
                    {
                        if (allTests[name].renameMap.ContainsKey(renamedMachineName))
                        {
                            Console.WriteLine("Internal Error");
                            Environment.Exit(-1);
                        }
                        else
                        {
                            allTests[name].renameMap.Add(renamedMachineName, impName);
                        }
                    }
                    else
                    {
                        allTests[name] = new TestCaseInfo();
                        allTests[name].renameMap.Add(renamedMachineName, impName);
                    }
                }
            }

            terms = GetBin(factBins, "CSharpDependsOn");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var currFileName = ((Cnst)it.Current).GetStringValue();
                    currFileName = Path.GetFileNameWithoutExtension(currFileName);
                    it.MoveNext();

                    if (it.Current.NodeKind == NodeKind.Id)
                    {
                        var name = ((Id)it.Current).Name;
                        if (name == "NIL")
                        {
                            if (!dependsOn.ContainsKey(currFileName))
                            {
                                dependsOn[currFileName] = new List <string>();
                            }
                        }
                    }
                    else
                    {
                        var dOn = ((Cnst)it.Current).GetStringValue();
                        dOn = Path.GetFileNameWithoutExtension(dOn);

                        if (dependsOn.ContainsKey(currFileName))
                        {
                            dependsOn[currFileName].Add(dOn);
                        }
                        else
                        {
                            dependsOn[currFileName] = new List <string>();
                            dependsOn[currFileName].Add(dOn);
                        }
                    }
                }
            }

            terms = GetBin(factBins, "CSharpSafeMap");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var renamedMachineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var isSafe = (it.Current as Id).Name == PData.Cnst_True.Node.Name;

                    if (allTests.ContainsKey(name))
                    {
                        if (allTests[name].isSafeMap.ContainsKey(renamedMachineName))
                        {
                            Console.WriteLine("Internal Error");
                            Environment.Exit(-1);
                        }
                        else
                        {
                            allTests[name].isSafeMap.Add(renamedMachineName, isSafe);
                        }
                    }
                    else
                    {
                        allTests[name] = new TestCaseInfo();
                        allTests[name].isSafeMap.Add(renamedMachineName, isSafe);
                    }
                }
            }

            terms = GetBin(factBins, "CSharpMonitorMap");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var newSpecMachineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var impMachine = ((Cnst)it.Current).GetStringValue();

                    if (allTests.ContainsKey(name))
                    {
                        if (allTests[name].specMachineMap.ContainsKey(newSpecMachineName))
                        {
                            allTests[name].specMachineMap[newSpecMachineName].Add(impMachine);
                        }
                        else
                        {
                            allTests[name].specMachineMap[newSpecMachineName] = new List <string>();
                            allTests[name].specMachineMap[newSpecMachineName].Add(impMachine);
                        }
                    }
                    else
                    {
                        allTests[name] = new TestCaseInfo();
                        allTests[name].specMachineMap[newSpecMachineName] = new List <string>();
                        allTests[name].specMachineMap[newSpecMachineName].Add(impMachine);
                    }
                }
            }

            terms = GetBin(factBins, "CSharpInterfaceMap");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var iname = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var evname = it.Current is Cnst ? ((Cnst)it.Current).GetStringValue() : "halt";


                    var testInfo = new TestCaseInfo();
                    if (allTests.ContainsKey(name))
                    {
                        if (allTests[name].interfaceMap.ContainsKey(iname))
                        {
                            allTests[name].interfaceMap[iname].Add(evname);
                        }
                        else
                        {
                            allTests[name].interfaceMap[iname] = new List <string>();
                            allTests[name].interfaceMap[iname].Add(evname);
                        }
                    }
                    else
                    {
                        allTests[name] = new TestCaseInfo();
                        allTests[name].interfaceMap[iname] = new List <string>();
                        allTests[name].interfaceMap[iname].Add(evname);
                    }
                }
            }

            terms = GetBin(factBins, "CSharpEventActionsRefinementTest");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var eventName = ((Cnst)it.Current).GetStringValue();

                    var lhsName = string.Format("{0}LHS", name);
                    var rhsName = string.Format("{0}RHS", name);
                    allTests[lhsName].sendActions.Add(eventName);
                    allTests[rhsName].sendActions.Add(eventName);
                }
            }

            terms = GetBin(factBins, "CSharpInterfaceActionsRefinementTest");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var eventName = ((Cnst)it.Current).GetStringValue();

                    var lhsName = string.Format("{0}LHS", name);
                    var rhsName = string.Format("{0}RHS", name);
                    allTests[lhsName].createActions.Add(eventName);
                    allTests[rhsName].createActions.Add(eventName);
                }
            }
        }
Example #9
0
 public static string GetName(FuncTerm ft, int nameIndex)
 {
     return(((Cnst)GetArgByIndex(ft, nameIndex)).GetStringValue());
 }
Example #10
0
        private void GenerateProgramData()
        {
            LinkedList <AST <FuncTerm> > terms = GetBin(factBins, "FileInfo");

            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    Node fun = it.Current;
                    it.MoveNext();
                    var    fileInfo = it.Current as Cnst;
                    string fileName = null;
                    if (fileInfo != null)
                    {
                        fileName = fileInfo.GetStringValue();
                        if (compiler.Options.shortFileNames)
                        {
                            fileName = Path.GetFileName(fileName);
                        }
                    }
                    funToFileName[Factory.Instance.ToAST(fun)] = fileName;
                }
            }

            terms = GetBin(factBins, "EventDecl");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    Node bound = it.Current;
                    it.MoveNext();
                    var payloadType = (FuncTerm)(it.Current.NodeKind == NodeKind.Id ? PTypeNull.Node : it.Current);
                    if (bound.NodeKind == NodeKind.Id)
                    {
                        allEvents[name] = new EventInfo(payloadType);
                    }
                    else
                    {
                        var  ft                  = (FuncTerm)bound;
                        var  maxInstances        = (int)((Cnst)GetArgByIndex(ft, 0)).GetNumericValue().Numerator;
                        bool maxInstancesAssumed = ((Id)ft.Function).Name == "AssumeMaxInstances";
                        allEvents[name] = new EventInfo(maxInstances, maxInstancesAssumed, payloadType);
                    }
                }
            }

            terms = GetBin(factBins, "EnumTypeDef");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var strIter = it.Current as FuncTerm;
                    it.MoveNext();
                    var valIter   = it.Current as FuncTerm;
                    var constants = new Dictionary <string, int>();
                    if (valIter == null)
                    {
                        var val = 0;
                        while (strIter != null)
                        {
                            string constant = (GetArgByIndex(strIter, 0) as Cnst).GetStringValue();
                            constants[constant] = val;
                            strIter             = GetArgByIndex(strIter, 1) as FuncTerm;
                            val++;
                        }
                    }
                    else
                    {
                        while (strIter != null)
                        {
                            string   constant = (GetArgByIndex(strIter, 0) as Cnst).GetStringValue();
                            Rational val      = (GetArgByIndex(valIter, 0) as Cnst).GetNumericValue();
                            constants[constant] = (int)val.Numerator;
                            strIter             = GetArgByIndex(strIter, 1) as FuncTerm;
                            valIter             = GetArgByIndex(valIter, 1) as FuncTerm;
                        }
                    }

                    allEnums[name] = constants;
                }
            }

            terms = GetBin(factBins, "MachineDecl");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string machineName = ((Cnst)it.Current).GetStringValue();
                    allMachines[machineName] = new MachineInfo();
                }
            }

            terms = GetBin(factBins, "MachineKind");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string machineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    allMachines[machineName].type = ((Id)it.Current).Name;
                }
            }

            terms = GetBin(factBins, "MachineCard");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string machineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    Node bound = it.Current;
                    if (bound.NodeKind != NodeKind.Id)
                    {
                        var ft = (FuncTerm)bound;
                        allMachines[machineName].maxQueueSize        = (int)((Cnst)GetArgByIndex(ft, 0)).GetNumericValue().Numerator;
                        allMachines[machineName].maxQueueSizeAssumed = ((Id)ft.Function).Name == "AssumeMaxInstances";
                    }
                }
            }

            terms = GetBin(factBins, "MachineStart");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string machineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    allMachines[machineName].initStateName = GetNameFromQualifiedName(machineName, (FuncTerm)it.Current);
                }
            }

            terms = GetBin(factBins, "ObservesDecl");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string machineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    allMachines[machineName].observesEvents.Add(((Cnst)it.Current).GetStringValue());
                }
            }

            terms = GetBin(factBins, "MachineReceives");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string machineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    string eventName;
                    if (it.Current.NodeKind == NodeKind.Id)
                    {
                        string name = ((Id)it.Current).Name;
                        if (name == "ALL")
                        {
                            allMachines[machineName].receiveSet = null;
                        }
                        else
                        {
                            eventName = HaltEvent;
                            allMachines[machineName].receiveSet.Add(eventName);
                        }
                    }
                    else
                    {
                        eventName = ((Cnst)it.Current).GetStringValue();
                        allMachines[machineName].receiveSet.Add(eventName);
                    }
                }
            }

            terms = GetBin(factBins, "MachineSends");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string machineName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    string eventName;
                    if (it.Current.NodeKind == NodeKind.Id)
                    {
                        string name = ((Id)it.Current).Name;
                        if (name == "ALL")
                        {
                            allMachines[machineName].sendsSet = null;
                        }
                        else
                        {
                            eventName = HaltEvent;
                            allMachines[machineName].sendsSet.Add(eventName);
                        }
                    }
                    else
                    {
                        eventName = ((Cnst)it.Current).GetStringValue();
                        allMachines[machineName].sendsSet.Add(eventName);
                    }
                }
            }

            terms = GetBin(factBins, "VarDecl");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string varName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    string machineName = ((Cnst)it.Current).GetStringValue();
                    Dictionary <string, VariableInfo> varTable = allMachines[machineName].localVariableToVarInfo;
                    it.MoveNext();
                    var type = (FuncTerm)it.Current;
                    varTable[varName] = new VariableInfo(type);
                }
            }

            var translatedBody = new Dictionary <AST <Node>, Node>();

            terms = GetBin(factBins, "TranslatedBody");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    AST <Node> cntxt = Factory.Instance.ToAST(it.Current);
                    it.MoveNext();
                    Node newStmt = it.Current;
                    translatedBody[cntxt] = newStmt;
                }
            }

            terms = GetBin(factBins, "FunDecl");
            foreach (AST <FuncTerm> term in terms)
            {
                AST <Node> termAlias = Factory.Instance.ToAST(termToAlias[term]);
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string funName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var owner = it.Current as Cnst;
                    it.MoveNext();
                    bool isModel = ((Id)it.Current).Name == "MODEL";
                    it.MoveNext();
                    var parameters = it.Current as FuncTerm;
                    it.MoveNext();
                    AST <FuncTerm> returnTypeName = it.Current is Id ? PTypeNull : (AST <FuncTerm>)Factory.Instance.ToAST(it.Current);
                    it.MoveNext();
                    var locals = it.Current as FuncTerm;
                    it.MoveNext();
                    Node body    = translatedBody[termAlias];
                    var  funInfo = new FunInfo(false, parameters, returnTypeName, locals, body);
                    if (owner != null)
                    {
                        string      machineName = owner.GetStringValue();
                        MachineInfo machineInfo = allMachines[machineName];
                        machineInfo.funNameToFunInfo[funName] = funInfo;
                    }
                    else
                    {
                        allGlobalFuns[funName] = funInfo;
                    }
                }
            }

            terms = GetBin(factBins, "FunProtoDecl");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string funName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var parameters = it.Current as FuncTerm;
                    it.MoveNext();
                    AST <FuncTerm> returnTypeName = it.Current is Id ? PTypeNull : (AST <FuncTerm>)Factory.Instance.ToAST(it.Current);
                    if (!allGlobalFuns.ContainsKey(funName))
                    {
                        allGlobalFuns.Add(funName, new FunInfo(parameters, returnTypeName));
                    }
                }
            }

            var anonFunCounter       = new Dictionary <string, int>();
            var anonFunCounterStatic = 0;

            foreach (string x in allMachines.Keys)
            {
                anonFunCounter[x] = 0;
            }

            terms = GetBin(factBins, "AnonFunDecl");
            foreach (AST <FuncTerm> term in terms)
            {
                AST <Node> termAlias = Factory.Instance.ToAST(termToAlias[term]);
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var ownerMachineName = it.Current as Cnst;
                    it.MoveNext();
                    var ownerFunName = it.Current as Cnst;
                    it.MoveNext();
                    var locals = it.Current as FuncTerm;
                    it.MoveNext();
                    Node body = translatedBody[termAlias];
                    it.MoveNext();
                    var envVars = it.Current as FuncTerm;
                    if (ownerMachineName == null)
                    {
                        string funName = "AnonFunStatic" + anonFunCounterStatic;
                        allGlobalFuns[funName]   = new FunInfo(true, envVars, PTypeNull, locals, body);
                        anonFunToName[termAlias] = funName;
                        anonFunCounterStatic++;
                    }
                    else
                    {
                        string      machineName = ownerMachineName.GetStringValue();
                        MachineInfo machineInfo = allMachines[machineName];
                        string      funName     = "AnonFun" + anonFunCounter[machineName];
                        machineInfo.funNameToFunInfo[funName] = new FunInfo(true, envVars, PTypeNull, locals, body);
                        anonFunToName[termAlias] = funName;
                        anonFunCounter[machineName]++;
                    }
                }
            }

            terms = GetBin(factBins, "StateDecl");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var qualifiedStateName = (FuncTerm)it.Current;
                    it.MoveNext();
                    string ownerName = ((Cnst)it.Current).GetStringValue();
                    string stateName = GetNameFromQualifiedName(ownerName, qualifiedStateName);
                    it.MoveNext();
                    string entryActionName = it.Current.NodeKind == NodeKind.Cnst
                        ? ((Cnst)it.Current).GetStringValue()
                        : anonFunToName[Factory.Instance.ToAST(it.Current)];
                    it.MoveNext();
                    string exitFunName = it.Current.NodeKind == NodeKind.Cnst
                        ? ((Cnst)it.Current).GetStringValue()
                        : anonFunToName[Factory.Instance.ToAST(it.Current)];
                    it.MoveNext();
                    var    temperature = StateTemperature.WARM;
                    string t           = ((Id)it.Current).Name;
                    if (t == "HOT")
                    {
                        temperature = StateTemperature.HOT;
                    }
                    else if (t == "COLD")
                    {
                        temperature = StateTemperature.COLD;
                    }
                    Dictionary <string, StateInfo> stateTable = allMachines[ownerName].stateNameToStateInfo;
                    stateTable[stateName] = new StateInfo(
                        ownerName,
                        entryActionName,
                        exitFunName,
                        temperature,
                        GetPrintedNameFromQualifiedName(qualifiedStateName));
                }
            }

            terms = GetBin(factBins, "TransDecl");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var       stateDecl             = (FuncTerm)it.Current;
                    var       qualifiedStateName    = (FuncTerm)GetArgByIndex(stateDecl, 0);
                    string    stateOwnerMachineName = GetName(stateDecl, 1);
                    string    stateName             = GetNameFromQualifiedName(stateOwnerMachineName, qualifiedStateName);
                    StateInfo stateTable            = allMachines[stateOwnerMachineName].stateNameToStateInfo[stateName];
                    it.MoveNext();
                    string eventName;
                    if (it.Current.NodeKind == NodeKind.Id)
                    {
                        string name = ((Id)it.Current).Name;
                        if (name == "NULL")
                        {
                            eventName = NullEvent;
                            stateTable.hasNullTransition = true;
                        }
                        else
                        {
                            // name == "HALT"
                            eventName = HaltEvent;
                        }
                    }
                    else
                    {
                        eventName = ((Cnst)it.Current).GetStringValue();
                    }
                    it.MoveNext();
                    string targetStateName = GetNameFromQualifiedName(stateOwnerMachineName, (FuncTerm)it.Current);
                    it.MoveNext();
                    if (it.Current.NodeKind == NodeKind.Cnst)
                    {
                        string exitFunName = ((Cnst)it.Current).GetStringValue();
                        stateTable.transitions[eventName] = new TransitionInfo(targetStateName, exitFunName);
                    }
                    else if (it.Current.NodeKind == NodeKind.Id && (it.Current as Id).Name == "PUSH")
                    {
                        stateTable.transitions[eventName] = new TransitionInfo(targetStateName);
                    }
                    else
                    {
                        string exitFunName = anonFunToName[Factory.Instance.ToAST(it.Current)];
                        stateTable.transitions[eventName] = new TransitionInfo(targetStateName, exitFunName);
                    }
                }
            }

            terms = GetBin(factBins, "DoDecl");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var       stateDecl             = (FuncTerm)it.Current;
                    var       qualifiedStateName    = (FuncTerm)GetArgByIndex(stateDecl, 0);
                    string    stateOwnerMachineName = GetName(stateDecl, 1);
                    string    stateName             = GetNameFromQualifiedName(stateOwnerMachineName, qualifiedStateName);
                    StateInfo stateTable            = allMachines[stateOwnerMachineName].stateNameToStateInfo[stateName];
                    it.MoveNext();
                    string eventName;
                    if (it.Current.NodeKind == NodeKind.Id)
                    {
                        string name = ((Id)it.Current).Name;
                        eventName = name == "NULL" ? NullEvent : HaltEvent;
                    }
                    else
                    {
                        eventName = ((Cnst)it.Current).GetStringValue();
                    }
                    it.MoveNext();
                    Node action = it.Current;
                    if (action.NodeKind == NodeKind.Cnst)
                    {
                        stateTable.dos[eventName] = ((Cnst)action).GetStringValue();
                    }
                    else if (action.NodeKind == NodeKind.Id && (action as Id).Name == "DEFER")
                    {
                        stateTable.deferredEvents.Add(eventName);
                    }
                    else if (action.NodeKind == NodeKind.Id && (action as Id).Name == "IGNORE")
                    {
                        stateTable.dos[eventName] = "ignore";
                    }
                    else
                    {
                        stateTable.dos[eventName] = anonFunToName[Factory.Instance.ToAST(action)];
                    }
                }
            }

            terms = GetBin(factBins, "Annotation");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    FuncTerm annotationContext = it.Current.NodeKind == NodeKind.Id
                        ? aliasToTerm[Factory.Instance.ToAST(it.Current)]
                        : (FuncTerm)it.Current;
                    string annotationContextKind = ((Id)annotationContext.Function).Name;
                    if (annotationContextKind != "FunDecl")
                    {
                        continue;
                    }

                    string ownerName = GetOwnerName(annotationContext, 1);
                    string funName   = GetName(annotationContext, 0);
                    it.MoveNext();
                    string annotation = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    if (annotation == "invokescheduler")
                    {
                        if (ownerName == null)
                        {
                            allGlobalFuns[funName].invokeSchedulerFuns.Add(it.Current);
                        }
                        else
                        {
                            allMachines[ownerName].funNameToFunInfo[funName].invokeSchedulerFuns.Add(it.Current);
                        }
                    }
                    else if (annotation == "printvalue")
                    {
                        var indexCnst = it.Current as Cnst;
                        if (indexCnst != null)
                        {
                            string arg = indexCnst.GetStringValue();
                            if (ownerName == null)
                            {
                                allGlobalFuns[funName].printArgs.Add(arg);
                            }
                            else
                            {
                                allMachines[ownerName].funNameToFunInfo[funName].printArgs.Add(arg);
                            }
                        }
                    }
                    else if (annotation == "invokeplugin")
                    {
                        if (ownerName == null)
                        {
                            allGlobalFuns[funName].invokePluginFuns.Add(it.Current);
                        }
                        else
                        {
                            allMachines[ownerName].funNameToFunInfo[funName].invokePluginFuns.Add(it.Current);
                        }
                    }
                }
            }

            terms = GetBin(factBins, "ExportedEvent");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string eventName = ((Cnst)it.Current).GetStringValue();
                    exportedEvents.Add(eventName);
                }
            }

            terms = GetBin(factBins, "MaxNumLocals");
            foreach (AST <FuncTerm> term in terms)
            {
                using (IEnumerator <Node> it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    AST <Node> typingContextAlias = Factory.Instance.ToAST(it.Current);
                    FuncTerm   typingContext      = aliasToTerm[typingContextAlias];
                    string     typingContextKind  = ((Id)typingContext.Function).Name;
                    it.MoveNext();
                    var maxNumLocals = (int)((Cnst)it.Current).GetNumericValue().Numerator;

                    if (typingContextKind == "FunDecl")
                    {
                        string ownerName = GetOwnerName(typingContext, 1);
                        string funName   = GetName(typingContext, 0);
                        if (ownerName == null)
                        {
                            allGlobalFuns[funName].maxNumLocals = maxNumLocals;
                        }
                        else
                        {
                            allMachines[ownerName].funNameToFunInfo[funName].maxNumLocals = maxNumLocals;
                        }
                    }
                    else
                    {
                        // typingContextKind == "AnonFunDecl"
                        string ownerName = GetOwnerName(typingContext, 0);
                        string funName   = anonFunToName[typingContextAlias];
                        if (ownerName == null)
                        {
                            allGlobalFuns[funName].maxNumLocals = maxNumLocals;
                        }
                        else
                        {
                            allMachines[ownerName].funNameToFunInfo[funName].maxNumLocals = maxNumLocals;
                        }
                    }
                }
            }

            if (compiler.Options.liveness != LivenessOption.None)
            {
                foreach (string machineName in allMachines.Keys)
                {
                    if (!allMachines[machineName].IsSpec)
                    {
                        continue;
                    }

                    MachineInfo machineInfo = allMachines[machineName];
                    var         initialSet  = new List <string>();
                    foreach (string stateName in ComputeReachableStates(machineName, machineInfo, new[] { machineInfo.initStateName }))
                    {
                        if (machineInfo.stateNameToStateInfo[stateName].IsWarm)
                        {
                            continue;
                        }

                        if (machineInfo.stateNameToStateInfo[stateName].IsHot)
                        {
                            machineInfo.specType = SpecType.FINALLY;
                            continue;
                        }

                        initialSet.Add(stateName);
                    }
                    foreach (string stateName in ComputeReachableStates(machineName, machineInfo, initialSet))
                    {
                        if (machineInfo.stateNameToStateInfo[stateName].IsHot)
                        {
                            machineInfo.specType = SpecType.REPEATEDLY;
                            break;
                        }
                    }
                }

                if (allMachines.Values.All(x => !x.IsSpec || x.specType == SpecType.SAFETY))
                {
                    compiler.Options.liveness = LivenessOption.None;
                }
            }
        }
Example #11
0
 public VariableInfo(FuncTerm type)
 {
     this.type = type;
 }
Example #12
0
        private void GenerateProgramData(AST <Model> model)
        {
            var factBins = new Dictionary <string, LinkedList <AST <FuncTerm> > >();

            model.FindAll(
                new NodePred[]
            {
                NodePredFactory.Instance.Star,
                NodePredFactory.Instance.MkPredicate(NodeKind.ModelFact)
            },
                (path, n) =>
            {
                var mf      = (ModelFact)n;
                FuncTerm ft = (FuncTerm)mf.Match;
                GetBin(factBins, ft).AddLast((AST <FuncTerm>)Factory.Instance.ToAST(ft));
            });

            funToFileName        = new Dictionary <AST <Node>, string>();
            allEvents            = new Dictionary <string, EventInfo>();
            allEnums             = new Dictionary <string, List <Tuple <string, int> > >();
            allEvents[HaltEvent] = new EventInfo(1, false, PTypeNull.Node);
            allEvents[NullEvent] = new EventInfo(1, false, PTypeNull.Node);
            allMachines          = new Dictionary <string, MachineInfo>();
            allStaticFuns        = new Dictionary <string, FunInfo>();
            linkMap = new Dictionary <string, string>();

            LinkedList <AST <FuncTerm> > terms;

            terms = GetBin(factBins, "FileInfo");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var fun = it.Current;
                    it.MoveNext();
                    var    fileInfo = it.Current as Cnst;
                    string fileName = null;
                    if (fileInfo != null)
                    {
                        fileName = fileInfo.GetStringValue();
                        if (compiler.Options.shortFileNames)
                        {
                            fileName = Path.GetFileName(fileName);
                        }
                    }
                    funToFileName[Factory.Instance.ToAST(fun)] = fileName;
                }
            }

            terms = GetBin(factBins, "EventDecl");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var bound = it.Current;
                    it.MoveNext();
                    var payloadType = (FuncTerm)(it.Current.NodeKind == NodeKind.Id ? PTypeNull.Node : it.Current);
                    if (bound.NodeKind == NodeKind.Id)
                    {
                        allEvents[name] = new EventInfo(payloadType);
                    }
                    else
                    {
                        var ft                  = (FuncTerm)bound;
                        var maxInstances        = (int)((Cnst)GetArgByIndex(ft, 0)).GetNumericValue().Numerator;
                        var maxInstancesAssumed = ((Id)ft.Function).Name == "AssumeMaxInstances";
                        allEvents[name] = new EventInfo(maxInstances, maxInstancesAssumed, payloadType);
                    }
                }
            }

            terms = GetBin(factBins, "EnumTypeDef");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var name = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    FuncTerm strIter = it.Current as FuncTerm;
                    it.MoveNext();
                    FuncTerm valIter   = it.Current as FuncTerm;
                    var      constants = new List <Tuple <string, int> >();
                    if (valIter == null)
                    {
                        var val = 0;
                        while (strIter != null)
                        {
                            var constant = (GetArgByIndex(strIter, 0) as Cnst).GetStringValue();
                            constants.Add(Tuple.Create <string, int>(constant, val));
                            strIter = GetArgByIndex(strIter, 1) as FuncTerm;
                            val++;
                        }
                    }
                    else
                    {
                        while (strIter != null)
                        {
                            var constant = (GetArgByIndex(strIter, 0) as Cnst).GetStringValue();
                            var val      = (GetArgByIndex(valIter, 0) as Cnst).GetNumericValue();
                            constants.Add(Tuple.Create <string, int>(constant, (int)val.Numerator));
                            strIter = GetArgByIndex(strIter, 1) as FuncTerm;
                            valIter = GetArgByIndex(valIter, 1) as FuncTerm;
                        }
                    }
                    allEnums[name] = constants;
                }
            }

            terms = GetBin(factBins, "MachineDecl");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var machineName = ((Cnst)it.Current).GetStringValue();
                    allMachines[machineName] = new MachineInfo();
                    it.MoveNext();
                    allMachines[machineName].type = ((Id)it.Current).Name;
                    it.MoveNext();
                    var bound = it.Current;
                    if (bound.NodeKind != NodeKind.Id)
                    {
                        var ft = (FuncTerm)bound;
                        allMachines[machineName].maxQueueSize        = (int)((Cnst)GetArgByIndex(ft, 0)).GetNumericValue().Numerator;
                        allMachines[machineName].maxQueueSizeAssumed = ((Id)ft.Function).Name == "AssumeMaxInstances";
                    }
                    it.MoveNext();
                    allMachines[machineName].initStateName = GetNameFromQualifiedName(machineName, (FuncTerm)it.Current);
                }
            }

            terms = GetBin(factBins, "ObservesDecl");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var machineDecl = (FuncTerm)it.Current;
                    var machineName = GetName(machineDecl, 0);
                    it.MoveNext();
                    allMachines[machineName].observesEvents.Add(((Cnst)it.Current).GetStringValue());
                }
            }

            terms = GetBin(factBins, "VarDecl");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var varName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var machineDecl = (FuncTerm)it.Current;
                    var machineName = GetName(machineDecl, 0);
                    var varTable    = allMachines[machineName].localVariableToVarInfo;
                    it.MoveNext();
                    var type = (FuncTerm)it.Current;
                    varTable[varName] = new VariableInfo(type);
                }
            }

            Dictionary <AST <Node>, Node> translatedBody = new Dictionary <AST <Node>, Node>();

            terms = GetBin(factBins, "TranslatedBody");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var cntxt = Factory.Instance.ToAST(it.Current);
                    it.MoveNext();
                    var newStmt = it.Current;
                    translatedBody[cntxt] = newStmt;
                }
            }

            terms = GetBin(factBins, "FunDecl");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    string funName = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    var owner = it.Current;
                    it.MoveNext();
                    var isModel = ((Id)it.Current).Name == "MODEL";
                    it.MoveNext();
                    var parameters = it.Current as FuncTerm;
                    it.MoveNext();
                    var returnTypeName = it.Current is Id ? PTypeNull : (AST <FuncTerm>)Factory.Instance.ToAST(it.Current);
                    it.MoveNext();
                    var locals = it.Current as FuncTerm;
                    it.MoveNext();
                    var body    = translatedBody[term];
                    var funInfo = new FunInfo(false, parameters, returnTypeName, locals, body);
                    if (owner is FuncTerm)
                    {
                        var machineDecl = (FuncTerm)owner;
                        var machineName = GetName(machineDecl, 0);
                        var machineInfo = allMachines[machineName];
                        machineInfo.funNameToFunInfo[funName] = funInfo;
                    }
                    else
                    {
                        allStaticFuns[funName] = funInfo;
                    }
                }
            }

            this.anonFunToName = new Dictionary <AST <Node>, string>();
            var anonFunCounter       = new Dictionary <string, int>();
            int anonFunCounterStatic = 0;

            foreach (var x in allMachines.Keys)
            {
                anonFunCounter[x] = 0;
            }
            terms = GetBin(factBins, "AnonFunDecl");
            foreach (var term in terms)
            {
                if (anonFunToName.ContainsKey(term))
                {
                    continue;
                }
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var machineDecl = it.Current as FuncTerm;
                    it.MoveNext();
                    var ownerFunName = it.Current as Cnst;
                    it.MoveNext();
                    var locals = it.Current as FuncTerm;
                    it.MoveNext();
                    var body = translatedBody[term];
                    it.MoveNext();
                    var envVars = it.Current as FuncTerm;
                    if (machineDecl == null)
                    {
                        var funName = "AnonFunStatic" + anonFunCounterStatic;
                        allStaticFuns[funName] = new FunInfo(true, envVars, PToZing.PTypeNull, locals, body);
                        anonFunToName[term]    = funName;
                        anonFunCounterStatic++;
                    }
                    else
                    {
                        var machineName = GetName(machineDecl, 0);
                        var machineInfo = allMachines[machineName];
                        var funName     = "AnonFun" + anonFunCounter[machineName];
                        machineInfo.funNameToFunInfo[funName] = new FunInfo(true, envVars, PToZing.PTypeNull, locals, body);
                        anonFunToName[term] = funName;
                        anonFunCounter[machineName]++;
                    }
                }
            }

            terms = GetBin(factBins, "StateDecl");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var qualifiedStateName = (FuncTerm)it.Current;
                    it.MoveNext();
                    var machineDecl = (FuncTerm)it.Current;
                    var ownerName   = GetName(machineDecl, 0);
                    var stateName   = GetNameFromQualifiedName(ownerName, qualifiedStateName);
                    it.MoveNext();
                    var entryActionName = it.Current.NodeKind == NodeKind.Cnst
                                            ? ((Cnst)it.Current).GetStringValue()
                                            : anonFunToName[Factory.Instance.ToAST(it.Current)];
                    it.MoveNext();
                    var exitFunName = it.Current.NodeKind == NodeKind.Cnst
                                            ? ((Cnst)it.Current).GetStringValue()
                                            : anonFunToName[Factory.Instance.ToAST(it.Current)];
                    it.MoveNext();
                    var temperature = StateTemperature.WARM;
                    var t           = ((Id)it.Current).Name;
                    if (t == "HOT")
                    {
                        temperature = StateTemperature.HOT;
                    }
                    else if (t == "COLD")
                    {
                        temperature = StateTemperature.COLD;
                    }
                    var stateTable = allMachines[ownerName].stateNameToStateInfo;
                    stateTable[stateName] = new StateInfo(ownerName, entryActionName, exitFunName, temperature, GetPrintedNameFromQualifiedName(qualifiedStateName));
                }
            }

            terms = GetBin(factBins, "TransDecl");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var stateDecl             = (FuncTerm)it.Current;
                    var qualifiedStateName    = (FuncTerm)GetArgByIndex(stateDecl, 0);
                    var stateOwnerMachineName = GetMachineName(stateDecl, 1);
                    var stateName             = GetNameFromQualifiedName(stateOwnerMachineName, qualifiedStateName);
                    var stateTable            = allMachines[stateOwnerMachineName].stateNameToStateInfo[stateName];
                    it.MoveNext();
                    string eventName;
                    if (it.Current.NodeKind == NodeKind.Id)
                    {
                        var name = ((Id)it.Current).Name;
                        if (name == "NULL")
                        {
                            eventName = NullEvent;
                            stateTable.hasNullTransition = true;
                        }
                        else
                        {
                            // name == "HALT"
                            eventName = HaltEvent;
                        }
                    }
                    else
                    {
                        eventName = ((Cnst)it.Current).GetStringValue();
                    }
                    it.MoveNext();
                    var targetStateName = GetNameFromQualifiedName(stateOwnerMachineName, (FuncTerm)it.Current);
                    it.MoveNext();
                    if (it.Current.NodeKind == NodeKind.Id)
                    {
                        stateTable.transitions[eventName] = new TransitionInfo(targetStateName);
                    }
                    else
                    {
                        var exitFunName = it.Current.NodeKind == NodeKind.Cnst
                                            ? ((Cnst)it.Current).GetStringValue()
                                            : anonFunToName[Factory.Instance.ToAST(it.Current)];
                        stateTable.transitions[eventName] = new TransitionInfo(targetStateName, exitFunName);
                    }
                }
            }

            terms = GetBin(factBins, "DoDecl");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    var stateDecl             = (FuncTerm)it.Current;
                    var qualifiedStateName    = (FuncTerm)GetArgByIndex(stateDecl, 0);
                    var stateOwnerMachineName = GetMachineName(stateDecl, 1);
                    var stateName             = GetNameFromQualifiedName(stateOwnerMachineName, qualifiedStateName);
                    var stateTable            = allMachines[stateOwnerMachineName].stateNameToStateInfo[stateName];
                    it.MoveNext();
                    string eventName;
                    if (it.Current.NodeKind == NodeKind.Id)
                    {
                        var name = ((Id)it.Current).Name;
                        if (name == "NULL")
                        {
                            eventName = NullEvent;
                        }
                        else
                        {
                            // name == "HALT"
                            eventName = HaltEvent;
                        }
                    }
                    else
                    {
                        eventName = ((Cnst)it.Current).GetStringValue();
                    }
                    it.MoveNext();
                    var action = it.Current;
                    if (action.NodeKind == NodeKind.Cnst)
                    {
                        stateTable.actions[eventName] = ((Cnst)action).GetStringValue();
                    }
                    else if (action.NodeKind == NodeKind.Id)
                    {
                        if (((Id)action).Name == "DEFER")
                        {
                            stateTable.deferredEvents.Add(eventName);
                        }
                        else
                        {
                            // ((Id)action).Name == "IGNORE"
                            stateTable.ignoredEvents.Add(eventName);
                            stateTable.actions[eventName] = "ignore";
                        }
                    }
                    else
                    {
                        stateTable.actions[eventName] = anonFunToName[Factory.Instance.ToAST(action)];
                    }
                }
            }

            terms = GetBin(factBins, "Annotation");
            foreach (var term in terms)
            {
                using (var it = term.Node.Args.GetEnumerator())
                {
                    it.MoveNext();
                    FuncTerm annotationContext     = (FuncTerm)it.Current;
                    string   annotationContextKind = ((Id)annotationContext.Function).Name;
                    if (annotationContextKind != "FunDecl")
                    {
                        continue;
                    }
                    string ownerName = GetOwnerName(annotationContext, 1, 0);
                    string funName   = GetName(annotationContext, 0);
                    it.MoveNext();
                    string annotation = ((Cnst)it.Current).GetStringValue();
                    it.MoveNext();
                    if (annotation == "invokescheduler")
                    {
                        if (ownerName == null)
                        {
                            allStaticFuns[funName].invokeSchedulerFuns.Add(it.Current);
                        }
                        else
                        {
                            allMachines[ownerName].funNameToFunInfo[funName].invokeSchedulerFuns.Add(it.Current);
                        }
                    }
                    else if (annotation == "printvalue")
                    {
                        Cnst indexCnst = it.Current as Cnst;
                        if (indexCnst != null)
                        {
                            string arg = indexCnst.GetStringValue();
                            if (ownerName == null)
                            {
                                allStaticFuns[funName].printArgs.Add(arg);
                            }
                            else
                            {
                                allMachines[ownerName].funNameToFunInfo[funName].printArgs.Add(arg);
                            }
                        }
                    }
                    else if (annotation == "invokeplugin")
                    {
                        if (ownerName == null)
                        {
                            allStaticFuns[funName].invokePluginFuns.Add(it.Current);
                        }
                        else
                        {
                            allMachines[ownerName].funNameToFunInfo[funName].invokePluginFuns.Add(it.Current);
                        }
                    }
                }
            }

            if (compiler.Options.liveness != LivenessOption.None)
            {
                foreach (var machineName in allMachines.Keys)
                {
                    if (!allMachines[machineName].IsSpec)
                    {
                        continue;
                    }
                    var           machineInfo = allMachines[machineName];
                    List <string> initialSet  = new List <string>();
                    foreach (var stateName in ComputeReachableStates(machineInfo, new string[] { machineInfo.initStateName }))
                    {
                        if (machineInfo.stateNameToStateInfo[stateName].IsWarm)
                        {
                            continue;
                        }
                        if (machineInfo.stateNameToStateInfo[stateName].IsHot)
                        {
                            machineInfo.specType = SpecType.FINALLY;
                            continue;
                        }
                        initialSet.Add(stateName);
                    }
                    foreach (var stateName in ComputeReachableStates(machineInfo, initialSet))
                    {
                        if (machineInfo.stateNameToStateInfo[stateName].IsHot)
                        {
                            machineInfo.specType = SpecType.REPEATEDLY;
                            break;
                        }
                    }
                }
                if (allMachines.Values.All(x => !x.IsSpec || x.specType == SpecType.SAFETY))
                {
                    compiler.Options.liveness = LivenessOption.None;
                }
            }
        }
Example #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FunctionCondition"/> class.
 /// </summary>
 /// <param name="label">The label.</param>
 /// <param name="id">The id.</param>
 /// <param name="attribute">The attribute.</param>
 /// <param name="value">The value.</param>
 public FunctionCondition(string label, Term id, FuncTerm attribute, Term value)
     : base(label, ConditionType.Function, id, attribute, value)
 {
 }
Example #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FunctionCondition"/> class.
 /// </summary>
 /// <param name="id">The id.</param>
 /// <param name="attribute">The attribute.</param>
 /// <param name="value">The value.</param>
 public FunctionCondition(Term id, FuncTerm attribute, Term value)
     : base("FunctionCondition", ConditionType.Function, id, attribute, value)
 {
 }
Example #15
0
            private AST <Node> ConstructType(FuncTerm type)
            {
                string typeKind = ((Id)type.Function).Name;

                if (typeKind == "BaseType")
                {
                    var primitiveType = ((Id)GetArgByIndex(type, 0)).Name;
                    if (primitiveType == "NULL")
                    {
                        var tmpVar = GetType();
                        AddTypeInitialization(MkZingAssign(tmpVar, MkZingCall(MkZingDot("PRT_TYPE", "PrtMkPrimitiveType"), MkZingDot("PRT_TYPE_KIND", "PRT_KIND_NULL"))));
                        return(tmpVar);
                    }
                    else if (primitiveType == "BOOL")
                    {
                        var tmpVar = GetType();
                        AddTypeInitialization(MkZingAssign(tmpVar, MkZingCall(MkZingDot("PRT_TYPE", "PrtMkPrimitiveType"), MkZingDot("PRT_TYPE_KIND", "PRT_KIND_BOOL"))));
                        return(tmpVar);
                    }
                    else if (primitiveType == "INT")
                    {
                        var tmpVar = GetType();
                        AddTypeInitialization(MkZingAssign(tmpVar, MkZingCall(MkZingDot("PRT_TYPE", "PrtMkPrimitiveType"), MkZingDot("PRT_TYPE_KIND", "PRT_KIND_INT"))));
                        return(tmpVar);
                    }
                    else if (primitiveType == "EVENT")
                    {
                        var tmpVar = GetType();
                        AddTypeInitialization(MkZingAssign(tmpVar, MkZingCall(MkZingDot("PRT_TYPE", "PrtMkPrimitiveType"), MkZingDot("PRT_TYPE_KIND", "PRT_KIND_EVENT"))));
                        return(tmpVar);
                    }
                    else if (primitiveType == "MACHINE")
                    {
                        var tmpVar = GetType();
                        AddTypeInitialization(MkZingAssign(tmpVar, MkZingCall(MkZingDot("PRT_TYPE", "PrtMkPrimitiveType"), MkZingDot("PRT_TYPE_KIND", "PRT_KIND_MACHINE"))));
                        return(tmpVar);
                    }
                    else
                    {
                        throw new NotSupportedException("Internal Error: Please report to P Developers");
                    }
                }
                else if (typeKind == "AnyType")
                {
                    var tmpVar = GetType();
                    AddTypeInitialization(MkZingAssign(tmpVar, MkZingCall(MkZingDot("PRT_TYPE", "PrtMkPrimitiveType"), MkZingDot("PRT_TYPE_KIND", "PRT_KIND_ANY"))));
                    return(tmpVar);
                }
                else if (typeKind == "NameType")
                {
                    var tmpVar = GetType();
                    AddTypeInitialization(MkZingAssign(tmpVar, MkZingCall(MkZingDot("PRT_TYPE", "PrtMkPrimitiveType"), MkZingDot("PRT_TYPE_KIND", "PRT_KIND_INT"))));
                    return(tmpVar);
                }
                else if (typeKind == "TupType")
                {
                    List <AST <Node> > memberTypes = new List <AST <Node> >();
                    while (type != null)
                    {
                        memberTypes.Add(PTypeToZingExpr((FuncTerm)GetArgByIndex(type, 0)));
                        type = GetArgByIndex(type, 1) as FuncTerm;
                    }
                    var tupleType = GetType();
                    AddTypeInitialization(MkZingAssign(tupleType, MkZingCall(MkZingDot("PRT_TYPE", "PrtMkTupType"), Factory.Instance.MkCnst(memberTypes.Count))));
                    for (int i = 0; i < memberTypes.Count; i++)
                    {
                        AddTypeInitialization(MkZingCallStmt(MkZingCall(MkZingDot("PRT_TYPE", "PrtSetFieldType"), tupleType, Factory.Instance.MkCnst(i), memberTypes[i])));
                    }
                    return(tupleType);
                }
                else if (typeKind == "NmdTupType")
                {
                    List <AST <Node> > memberNames = new List <AST <Node> >();
                    List <AST <Node> > memberTypes = new List <AST <Node> >();
                    while (type != null)
                    {
                        var typeField = (FuncTerm)GetArgByIndex(type, 0);
                        memberNames.Add(GetField(((Cnst)GetArgByIndex(typeField, 0)).GetStringValue()));
                        memberTypes.Add(PTypeToZingExpr((FuncTerm)GetArgByIndex(typeField, 1)));
                        type = GetArgByIndex(type, 1) as FuncTerm;
                    }
                    var tupleType = GetType();
                    AddTypeInitialization(MkZingAssign(tupleType, MkZingCall(MkZingDot("PRT_TYPE", "PrtMkNmdTupType"), Factory.Instance.MkCnst(memberTypes.Count))));
                    for (int i = 0; i < memberTypes.Count; i++)
                    {
                        AddTypeInitialization(MkZingCallStmt(MkZingCall(MkZingDot("PRT_TYPE", "PrtSetFieldName"), tupleType, Factory.Instance.MkCnst(i), memberNames[i])));
                        AddTypeInitialization(MkZingCallStmt(MkZingCall(MkZingDot("PRT_TYPE", "PrtSetFieldType"), tupleType, Factory.Instance.MkCnst(i), memberTypes[i])));
                    }
                    return(tupleType);
                }
                else if (typeKind == "SeqType")
                {
                    var innerType = PTypeToZingExpr((FuncTerm)GetArgByIndex(type, 0));
                    var seqType   = GetType();
                    AddTypeInitialization(MkZingAssign(seqType, MkZingCall(MkZingDot("PRT_TYPE", "PrtMkSeqType"), innerType)));
                    return(seqType);
                }
                else if (typeKind == "MapType")
                {
                    // typeKind == "MapType"
                    var domType = PTypeToZingExpr((FuncTerm)GetArgByIndex(type, 0));
                    var codType = PTypeToZingExpr((FuncTerm)GetArgByIndex(type, 1));
                    var mapType = GetType();
                    AddTypeInitialization(MkZingAssign(mapType, MkZingCall(MkZingDot("PRT_TYPE", "PrtMkMapType"), domType, codType)));
                    return(mapType);
                }
                else
                {
                    // its InterfaceType so consider it as machine type
                    var tmpVar = GetType();
                    AddTypeInitialization(MkZingAssign(tmpVar, MkZingCall(MkZingDot("PRT_TYPE", "PrtMkPrimitiveType"), MkZingDot("PRT_TYPE_KIND", "PRT_KIND_MACHINE"))));
                    return(tmpVar);
                }
            }