Esempio n. 1
0
        public IDecompilationResult Transform()
        {
            ProcessDescriptor      pd    = _code as ProcessDescriptor;
            FSMTransformerTemplate templ =
                new FSMTransformerTemplate(_context, _code, _instance, _arguments);
            Function body = templ.GetAlgorithm();

            _code.Implementation = body;
            foreach (var tup in templ.CoFSMs)
            {
                CoFSMInfo cfi = tup.Item2;
                cfi.Sensitivity.Add(cfi.CoStateSignal.Desc);
                FSMCombTemplate   cotempl = new FSMCombTemplate(cfi);
                Function          comb    = cotempl.GetAlgorithm();
                string            name    = _code.Name + "$comb";
                ProcessDescriptor pdcomb  = new ProcessDescriptor(name)
                {
                    Kind                  = Process.EProcessKind.Triggered,
                    Implementation        = comb,
                    GenuineImplementation = body,
                    Sensitivity           = cfi.Sensitivity.ToArray()
                };
                _code.Owner.AddChild(pdcomb, name);
            }
            return(templ);
        }
Esempio n. 2
0
        private void AddCoStates(int issuePoint, LocalVariableState lvState, IEnumerable <TAVerb> states)
        {
            Contract.Requires(states != null);

            TAVerb    first = states.First();
            CoFSMInfo cfi   = _coFSMs[first.Target];

            if (cfi.AddVerbs(issuePoint, lvState, states))
            {
                foreach (var tup in cfi.GetVerbs(issuePoint, lvState))
                {
                    TAVerb      tav = tup.Item1;
                    CoStateInfo csi = tup.Item2;
                    if (csi.StateAction == null)
                    {
                        InitCoState(tav, csi);
                        if (tav.During != null)
                        {
                            foreach (AbstractEvent ev in tav.During.Sensitivity)
                            {
                                cfi.Sensitivity.Add(((SignalBase)ev.Owner).Descriptor);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        private void InitializeCoFSMs()
        {
            ProcessDescriptor pd = _code as ProcessDescriptor;

            if (pd != null)
            {
                TransactingComponent.TATarget[] coFSMs = pd.Instance.GetCoFSMs();
                if (coFSMs != null)
                {
                    foreach (TransactingComponent.TATarget target in coFSMs)
                    {
                        CoFSMInfo cfi = new CoFSMInfo();
                        _coFSMs[target.Target] = cfi;
                        if (target.NeutralTA != null)
                        {
                            cfi.HasNeutralTA = true;
                            AddCoStates(-1, LocalVariableState.Empty, target.NeutralTA);
                        }
                        else
                        {
                            cfi.HasNeutralTA = false;
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        public void ImplementCoState(int issuePoint, IEnumerable <TAVerb> states, int step, IFunctionBuilder builder)
        {
            LocalVariableState lvState = _templ.ExportLocalVariableState();

            AddCoStates(issuePoint, lvState, states);
            TAVerb      first = states.First();
            CoFSMInfo   cfi   = _coFSMs[first.Target];
            var         tup   = cfi.GetVerbs(issuePoint, lvState).ElementAt(step);
            CoStateInfo csi   = tup.Item2;

            ImplementCoStateAction(cfi, csi, builder);
        }
Esempio n. 5
0
        private void ImplementCoStateAction(CoFSMInfo cfi, CoStateInfo csi, IFunctionBuilder builder)
        {
            builder.InlineCall(csi.StateAction, new Expression[0], new Variable[0], true);
            builder.Store(cfi.NextCoState, csi.StateExpr);

            /*
             * CoStateInfo next = csi.Next;
             * if (next == null && cfi.HasNeutralTA)
             *  next = cfi.FirstNeutral;
             * if (next != null)
             * {
             *  builder.Store(cfi.NextCoState, next.StateExpr);
             * }
             * */
        }
Esempio n. 6
0
        private void CreateCoStateValues()
        {
            foreach (var kvp in _coFSMs)
            {
                ITransactionSite target = kvp.Key;
                CoFSMInfo        cfi    = kvp.Value;

                string ename, fname;
                if (target.Name == null)
                {
                    ename = "E_" + target.Host.Descriptor.Name + "_CoState";
                    fname = "m_" + target.Host.Descriptor.Name + "_CoState";
                }
                else
                {
                    ename = "E_" + target.Host.Descriptor.Name + "_" + target.Name + "_CoState";
                    fname = "m_" + target.Host.Descriptor.Name + "_" + target.Name + "_CoState";
                }

                Type                tState  = CreateEnum(ename, cfi.TotalStates);
                TypeDescriptor      tdState = (TypeDescriptor)tState;
                ComponentDescriptor owner   = (ComponentDescriptor)_code.Owner;
                owner.AddChild(tdState, tdState.Name);

                Type         tStateSignal  = typeof(Signal <>).MakeGenericType(tState);
                FieldBuilder fbStateSignal = _tComponent.DefineField(fname, tStateSignal, FieldAttributes.Private);
                Array        enumValues    = tState.GetEnumValues();
                object       initVal;
                if (cfi.HasNeutralTA)
                {
                    initVal = cfi.FirstNeutral.StateValue;
                }
                else
                {
                    initVal = enumValues.GetValue(0);
                }
                var sdState = owner.CreateSignalInstance(fname, initVal);
                cfi.CoStateSignal           = new SignalRef(sdState, SignalRef.EReferencedProperty.Instance);
                cfi.CoStateType             = tStateSignal;
                cfi.NextCoState.PlaceHolder = new SignalRef(sdState, SignalRef.EReferencedProperty.Next);

                foreach (CoStateInfo csi in cfi.StateInfos)
                {
                    csi.StateValue            = enumValues.GetValue(csi.StateIndex);
                    csi.StateExpr.PlaceHolder = LiteralReference.CreateConstant(csi.StateValue);
                }
            }
        }
Esempio n. 7
0
 public FSMCombTemplate(CoFSMInfo cfi)
 {
     Contract.Requires(cfi != null);
     _cfi = cfi;
 }
Esempio n. 8
0
        protected override void DeclareAlgorithm()
        {
            Dissect();
            Initialize();
            InitializeCoFSMs();
            DecompileStates();
            CreateStateValues();
            CreateCoStateValues();

            SignalRef         curState    = new SignalRef(_stateSignal.Desc, SignalRef.EReferencedProperty.Cur);
            ProcessDescriptor pd          = (ProcessDescriptor)_code;
            Func <bool>       predFunc    = pd.Instance.Predicate;
            LiteralReference  predInstRef = LiteralReference.CreateConstant(predFunc.Target);
            StackElement      arg         = new StackElement(predInstRef, predFunc.Target, EVariability.ExternVariable);
            Expression        cond        = _templ.GetCallExpression(predFunc.Method, arg).Expr;

            Expression[] e0 = new Expression[0];
            Variable[]   v0 = new Variable[0];

            If(cond);
            {
                foreach (var kvp in _coFSMs)
                {
                    CoFSMInfo     cfi   = kvp.Value;
                    SignalRef     srCor = new SignalRef(cfi.CoStateSignal.Desc, SignalRef.EReferencedProperty.Cur);
                    Expression    lrCo  = new LiteralReference(srCor);
                    CaseStatement csc   = Switch(lrCo);
                    {
                        for (int i = 0; i < cfi.TotalStates; i++)
                        {
                            CoStateInfo csi        = cfi.StateInfos[i];
                            Expression  stateValue = LiteralReference.CreateConstant(csi.StateValue);
                            CoStateInfo csin       = csi.Next;
                            if (csin == null && cfi.HasNeutralTA)
                            {
                                csin = cfi.FirstNeutral;
                            }
                            Case(stateValue);
                            {
                                if (csin != null)
                                {
                                    ImplementCoStateAction(cfi, csin, this);
                                }
                                Break(csc);
                            }
                            EndCase();
                        }
                    }
                    EndSwitch();
                }

                CaseStatement cs = Switch(curState);
                {
                    IEnumerable <StateInfo> states = _stateLookup.Values.OrderBy(si => si.StateIndex);
                    foreach (StateInfo state in states)
                    {
                        Case(state.StateExpr.PlaceHolder);
                        {
                            InlineCall(state.StateFun, e0, v0, true);
                            Break(cs);
                        }
                        EndCase();
                    }
                }
                EndSwitch();
            }
            EndIf();
        }