Exemple #1
0
 private void FormatChannel(string template, VarDecl v, string name, string postfix)
 {
     if (_isSource)
     {
         app("const U2C_CHANNEL_DATA {0}{1}_DATA = // {2}", name, postfix, v.NiceName());
         app("    {{");
         app("        DBG_FIELD(\"{0}[{1}]\")	// name", v.NiceName(), postfix.TrimStart('_'));
         app("        {0},				// urgent", formatBool(v.Type.Urgent));
         app("        {0},				// broadcast", formatBool(v.Type.Broadcast));
         app("    }};");
         app();
         app("U2C_CHANNEL {0}{1} = // {2}", name, postfix, v.NiceName());
         app("    {{");
         app("        &{0}{1}_DATA,         // data", name, postfix);
         app("        NULL,              // firedcb");
         app("        NULL,              // context");
         app("        U2C_FALSE,              // cur_fired");
         app("        U2C_FALSE,              // prev_fired");
         app("    }};");
         app();
     }
     else
     {
         app("extern U2C_CHANNEL {0}{1}; // {2}", name, postfix, v.NiceName());
     }
 }
Exemple #2
0
        private void FormatSingleVarDeclaration(string typestr, string prefix, VarDecl vd)
        {
            string arpostfix = getArraryDecl(vd);
            string name      = setUniqueName(vd, String.Format("{0}_{1}_{2}{3}", typestr, prefix, vd.Name, arpostfix));

            app("        {0}{1} {2};", vd.Type.Const ? "const " : "", typestr, name);
        }
Exemple #3
0
    void VarNameDecl(uppaal2c.TypeDecl type)
    {
        string name; uppaal2c.Expression arg1 = null, arg2 = null;

        Ident(out name);
        uppaal2c.VarDecl vardecl = new uppaal2c.VarDecl(name, type); decls.Add(vardecl);
        if (la.kind == 11)
        {
            Get();
            Expression(out arg1);
            Expect(12);
            int low = 0, length = 0;
            if (!decls.getArrSizeValue(arg1, out low, out length))
            {
                throw new FatalError("Could not calculate array size!");
            }
            vardecl.IsArray   = true;
            vardecl.ArrLow    = low;
            vardecl.ArrLength = length;
        }
        if (la.kind == 13)
        {
            Get();
            Expression(out arg2);
            vardecl.Expr = arg2; vardecl.HasExpr = true;
        }
    }
Exemple #4
0
        private void FormatChannel(string template, VarDecl v)
        {
            string name = getVarDeclName(v, template);

            if (v.IsArray)
            {
                for (int i = v.ArrLow; i < (v.ArrLow + v.ArrLength); ++i)
                {
                    FormatChannel(template, v, name, String.Format("_{0}", i));
                }

                if (_isSource)
                {
                    app();
                    app("U2C_CHANNEL* const {0}_ARRAY[] = ", name);
                    app("    {{");
                    for (int i = v.ArrLow; i < (v.ArrLow + v.ArrLength); ++i)
                    {
                        app("        &{0}{1},", name, String.Format("_{0}", i));
                    }
                    app("        NULL");
                    app("    }};");
                    app();
                }
            }
            else
            {
                FormatChannel(template, v, name, "");
            }
        }
        public string generate(UpdateRule ur)
        {
            VarDecl vd = _decls.getVar(ur.VarName);

            if (vd == null)
            {
                throw new CodeGenException(String.Format("Unknown variable {0}!", ur.VarName));
            }

            string vname;

            if (vd.IsArray)
            {
                if (ur.Expr.Type != Expression.ExpType.Func ||
                    ur.Expr.Func != Expression.Funcs.ArrayIndex ||
                    ur.Expr.First.Type != Expression.ExpType.Var)
                {
                    throw new CodeGenException(String.Format("Array variable {0} not accessed by index!", vd.Name));
                }
                vd    = _decls.getVar(ur.Expr.First.Var);
                vname = String.Format("{0}[{1}]", _namer(vd), generate(ur.Expr.Second));
            }
            else
            {
                vname = _namer(vd);
            }

            return(String.Format("{0}({1}.{2}, ({3}));",
                                 getSetter(vd),
                                 _stateStructName,
                                 vname,
                                 generate(ur.Expr)));
        }
        public bool getVarValue(string varname, out int val)
        {
            val = 0;
            VarDecl res = getVar(varname);

            if (res == null)
            {
                throw new ParseException(String.Format("Value '{0}': unknown!", varname));
            }
            if (res.IsArray)
            {
                throw new ParseException(String.Format("Value '{0}': Indexers not yet support on consts!", varname));
            }

            if (res.Type.Type != VarType.Int)
            {
                return(false);
            }

            if (!res.HasExpr)
            {
                throw new ParseException(String.Format("Value '{0}': not initialized!", varname));
            }

            if (!res.Type.Const)
            {
                return(false);
            }

            return(getExprValue(res.Expr, out val));
        }
Exemple #7
0
        private Rule parseUpdateRule(string pname, string rule, Declarations decls)
        {
            int len   = 1;
            int equal = rule.IndexOf(":=");

            if (equal < 0)
            {
                equal = rule.IndexOf('=');
            }
            else
            {
                len = 2;
            }
            if (equal < 0)
            {
                throw new ParseException(String.Format("Invalid update rule '{0}'!", rule));
            }

            string varname = rule.Substring(0, equal).Trim();
            string upexpr  = rule.Substring(equal + len).Trim();

            VarDecl vd = decls.getVar(varname);

            if (vd == null)
            {
                throw new ParseException(String.Format("Unknown variable {0}!", varname));
            }

            string       expr     = String.Format("int _UPDATE_{0} = {1};", varname, upexpr);
            Declarations newdecls = parseDeclarations(expr, decls);
            Expression   e        = newdecls.getFirstDecl().Expr;

            e.fold(newdecls);
            return(new UpdateRule(varname, e));
        }
Exemple #8
0
        public TypeDecl GetType(Declarations symtab)
        {
            switch (Type)
            {
            case ExpType.ConstInt:
                return(new TypeDecl()
                {
                    Type = VarType.Int
                });

            case ExpType.ConstBool:
                return(new TypeDecl()
                {
                    Type = VarType.Int
                });

            case ExpType.Var:
                VarDecl v = symtab.getVar(Var);
                if (v == null)
                {
                    throw new ParseException(String.Format("Unknown variable {0}", Var));
                }
                return(v.Type);

            case ExpType.Func:
                // this should be improved later
                return(new TypeDecl()
                {
                    Type = VarType.Int
                });

            default:
                throw new ParseException("Internal error 1");
            }
        }
Exemple #9
0
        private static string getArraryDecl(VarDecl vd)
        {
            string postfix = "";

            if (vd.IsArray)
            {
                postfix = String.Format("[{0}]", vd.ArrLength);
            }
            return(postfix);
        }
Exemple #10
0
        private string getVarDeclName(VarDecl v, string template)
        {
            string name;

            if (_names.TryGetValue(v, out name))
            {
                return(name);
            }

            return(setUniqueName(v, String.Format("{0}_CHANNEL_VAR_{1}", template, v.Name)));
        }
        public VarDecl getVar(string name)
        {
            VarDecl vdef = _vdefs.SingleOrDefault(var => var.Name == name);

            if (vdef != null)
            {
                return(vdef);
            }
            if (Parent != null)
            {
                return(Parent.getVar(name));
            }
            return(null);
        }
        private string getSetter(VarDecl vd)
        {
            switch (vd.Type.Type)
            {
            case VarType.Channel:
                throw new CodeGenException("Channels cannot be used as variables!");

            case VarType.Clock:
                return("_U2C_SET_CLK");

            case VarType.Int:
                return("_U2C_SET_INT");

            default:
                throw new CodeGenException("Unknown variable type!");
            }
        }
Exemple #13
0
        private void FormatSingleVarInitialization(VarDecl vd, Declarations decl)
        {
            string name = getUniqueName(vd);

            if (!vd.HasExpr)
            {
                return;
            }

            int val;

            if (!decl.getExprValue(vd.Expr, out val))
            {
                throw new CodeGenException(String.Format("Cannot use non const variable!"));
            }

            app("\t{0}.{1} = {2};", StateStructName, name, val);
        }
        public VarWithScope getVarWithScope(string name)
        {
            VarDecl vdef = _vdefs.SingleOrDefault(var => var.Name == name);

            if (vdef != null)
            {
                return new VarWithScope()
                       {
                           var = vdef, decl = this
                       }
            }
            ;
            if (Parent != null)
            {
                return(Parent.getVarWithScope(name));
            }
            return(null);
        }
        public void Add(VarDecl vdecl)
        {
            verifyCleanName(vdecl.Name);

            _vdefs.Add(vdecl);
        }
 public void removeVar(VarDecl vd)
 {
     _vdefs.Remove(vd);
 }