Beispiel #1
0
                private void AddIfElseBranches(If control, JPF jpf, JMP jmp)
                {
                    Int32 endOfBlock = jmp.Index;

                    while (true)
                    {
                        Int32 newJpfIndex = jpf.Index;
                        JPF   newJpf      = _instructions[newJpfIndex] as JPF;
                        if (newJpf == null || newJpf.Index > endOfBlock)
                        {
                            control.AddElse(jpf.Index, endOfBlock);
                            return;
                        }

                        JMP newJmp = _instructions[newJpf.Index - 1] as JMP;
                        if (newJmp == null)
                        {
                            if (newJpf.Index == endOfBlock)
                            {
                                // if-elseif without jmp
                                _processed.Process(newJpf);
                                control.AddIf(newJpfIndex, newJpf.Index);
                            }
                            else
                            {
                                // if-else without jmp
                                control.AddElse(jpf.Index, endOfBlock);
                            }

                            return;
                        }

                        // Isn't our jump
                        if (newJmp.Index != endOfBlock)
                        {
                            control.AddElse(jpf.Index, endOfBlock);
                            return;
                        }

                        jpf = newJpf;
                        jmp = newJmp;
                        _processed.Process(jpf);
                        _processed.TryProcess(jmp);

                        control.AddIf(newJpfIndex, jpf.Index);

                        if (jpf.Index == endOfBlock)
                        {
                            return;
                        }
                    }
                }
Beispiel #2
0
        public void Inverse(JMP nextJmp)
        {
            if (_conditions.Count != 1)
            {
                throw new NotSupportedException($"Conditional jump already merged with an other one: {this}");
            }

            IJsmExpression expression = _conditions[0];

            if (!(expression is ILogicalExpression constExpression))
            {
                _conditions[0] = new Jsm.Expression.CAL.LogNot(expression);
            }
Beispiel #3
0
            private static void AppendItems(StringBuilder sb, IEnumerable <Object> items)
            {
                Int32 position = -1;
                JMP   lastItem = null;

                foreach (var item in items)
                {
                    if (lastItem != null)
                    {
                        throw new InvalidProgramException($"Unexpected jump: {lastItem}");
                    }

                    lastItem = item as JMP;
                    position = sb.Length;

                    sb.Append('\t').AppendLine(item.ToString());
                }

                if (lastItem != null)
                {
                    sb.Length = position;
                }
            }
Beispiel #4
0
                private Boolean TryMakeIf()
                {
                    JPF jpf = _begin;
                    JMP jmp = _instructions[_begin.Index - 1] as JMP;

                    If control = new If(_instructions, _index, _begin.Index);

                    _result.Add(control);

                    if (jmp == null)
                    {
                        // There is no JMP instruction. Simple if {}
                        return(true);
                    }

                    if (jmp.Index == jpf.Index)
                    {
                        // It isn't our jump, but an nested if. If { nested if{}<-}
                        return(true);
                    }

                    if (jmp.Index < jpf.Index)
                    {
                        // It isn't our jump, but an nested loop. If { nested while{}<-}
                        return(true);
                    }

                    if (jmp.Index < _index)
                    {
                        // It isn't our jump, but an nested goto. If { nested goto l;<-}
                        return(true);
                    }

                    _processed.Process(jmp);
                    AddIfElseBranches(control, jpf, jmp);
                    return(true);
                }