コード例 #1
0
        public EmitSyntax StackLoop(EmitSyntax emit, Action <EmitSyntax, Def <Locals> > emitLoopBody)
        {
            var labels = emit.Labels;
            var START  = labels.Generate();
            var END    = labels.Generate();

            var item = emit.Locals.Generate();

            emit
            .Local(item, itemType)
            .Label(START)
            .Ldloc(index.GetRef())
            .Ldc_I4(0)
            .Beq(END.GetRef())
            .Do(Pop)
            .Stloc(item.GetRef())
            ;

            emitLoopBody(emit, item);

            emit
            .Br(START.GetRef())
            .Label(END)
            .Nop();

            return(emit);
        }
コード例 #2
0
 private void GenerateCodeOrJump(Decision decision)
 {
     if (!strategy.TryInlineCode(decision))
     {
         emit.Br(GetNodeLabel(decision));
     }
 }
コード例 #3
0
        public EmitSyntax EmitLoopPass(EmitSyntax emit, bool loop)
        {
            emit.Label(START);

            FetchNext(emit, END);
            body(emit);

            if (loop)
            {
                emit.Br(START.GetRef());
            }

            return(emit
                   .Label(END)
                   .Nop());
        }
コード例 #4
0
        public ILNfaCompiler(AstNode node, EmitSyntax emit, ILCompilerSettings settings)
        {
            this.Scanner = new StemScanner();

            this.emit = emit;
            this.settings = settings;

            var labels = emit.Labels;
            NEXT_THREAD     = labels.Generate();
            RESUME_ALL      = labels.Generate();
            NEXT_INPUT      = labels.Generate();
            THREAD_SUSPEND  = labels.Generate();
            THREAD_DISPATCH = labels.Generate();

            var LABEL0      = labels.Generate();
            var LABEL1      = labels.Generate();
            var POSTCOMPILEINIT = labels.Generate();
            var POSTCOMPILEINIT_BACK = labels.Generate();

            var locals = emit.Locals;
            slots        = locals.Generate();
            labelIndexToLocation = locals.Generate();
            intTmp       = locals.Generate();
            threadTmp    = locals.Generate();

            INfaVM code = this;

            this.inputPump = new ArrayLoopGenerator(
                       valueType: emit.Types.Int32,
                       ldarray:   settings.LdInput,
                       body:
                           il => il
                               .Do(matchedStack.Clear)
                               .Br(RESUME_ALL.GetRef())
                        );

            this.runningStack   = new StackGenerator(emit, typeof(Thread));
            this.suspendedStack = new StackGenerator(emit, typeof(Thread));
            this.matchedStack   = new StackGenerator(emit, typeof(Thread));
            this.tmpStack       = new StackGenerator(emit, typeof(Thread), nullContainer: true);

            emit
                .Local(intTmp, emit.Types.Int32)
                .Local(slots, emit.Types.Import(typeof(int[])))
                .Local(labelIndexToLocation, emit.Types.Import(typeof(int[])))
                .Br(POSTCOMPILEINIT.GetRef())
                .Label(POSTCOMPILEINIT_BACK)
                .Local(threadTmp, emit.Types.Import(typeof(Thread)))
                .Ldloca(threadTmp.GetRef())
                .Initobj(emit.Types.Import(typeof(Thread)))
                .Do(inputPump.EmitInitialization)
                ;

            new RegularNfaVMCompiler().Compile(node, code);

            int LabelCount = Math.Max(1, indexToLabel.Count);

            this.runningStack.SetSize(LabelCount);
            this.suspendedStack.SetSize(LabelCount);
            this.matchedStack.SetSize(LabelCount);
            this.tmpStack.SetSize(LabelCount);

            emit
                .Label(POSTCOMPILEINIT)
                .Do(runningStack.Init)
                .Do(suspendedStack.Init)
                .Do(matchedStack.Init)
                .Do(tmpStack.Init)
                .Ldc_I4(slotCount)
                .Newarr(emit.Types.Int32)
                .Stloc(slots.GetRef())

                .Ldc_I4(LabelCount)
                .Newarr(emit.Types.Int32)
                .Stloc(labelIndexToLocation.GetRef())
                // Fill labelIndexToLocation with -1 :
                .Ldc_I4(LabelCount).Stloc(intTmp.GetRef())

                .Label(LABEL1)
                .Ldloc(intTmp.GetRef())
                .Ldc_I4_0()
                .Beq(LABEL0.GetRef())
                .Ldloc(intTmp.GetRef())
                .Ldc_I4(1)
                .Sub()
                .Stloc(intTmp.GetRef())
                .Ldloc(labelIndexToLocation.GetRef())
                .Ldloc(intTmp.GetRef())
                .Ldc_I4(int.MinValue)
                .Stelem_I4()
                .Br(LABEL1.GetRef())
                .Label(LABEL0)
                .Br(POSTCOMPILEINIT_BACK.GetRef());

            // Save thread as suspended (stack should already contain label index to suspend)
            emit.Label(THREAD_SUSPEND);
            StThreadValueByRuntimeLabelIndex();
            emit
                // Don't add thread if same thread (same label index
                // with current input location) already exists in a list.
                .Ldloc(labelIndexToLocation.GetRef())
                .Ldloca(threadTmp.GetRef())
                .Ldfld(LabelIndexField)
                .Ldelem_I4()
                .Ldloc(inputPump.Index.GetRef())
                .Beq(NEXT_THREAD.GetRef())

                // Mark label index as visited
                .Ldloc(labelIndexToLocation.GetRef())
                .Ldloca(threadTmp.GetRef())
                .Ldfld(LabelIndexField)
                .Ldloc(inputPump.Index.GetRef())
                .Stelem_I4()
                ;

            suspendedStack.PushFrom(emit, threadTmp);

            emit
                .Br(NEXT_THREAD.GetRef())

                .Label(RESUME_ALL)
                .Swap(suspendedStack.Stack, runningStack.Stack, tmpStack.Stack)
                .Swap(suspendedStack.Index, runningStack.Index, tmpStack.Index)
                .Label(NEXT_THREAD)
                ;

            runningStack
                .StackLoop(
                    emit,
                    (emit2, thread) =>
                    {
                        emit2
                            .Ldloca(thread.GetRef())
                            .Ldfld(LabelIndexField)
                            .Label(THREAD_DISPATCH)
                            .Switch(indexToLabel.Select(def => def.GetRef()).ToArray())
                            .Br(settings.FAILURE.GetRef());
                    })
                    ;
            emit
                .Br(NEXT_INPUT.GetRef())
                ;

            emit
                .Label(NEXT_INPUT)
                ;

            inputPump.EmitLoopPass(emit, false);

            emit
                // Check if there are matched threads:
                .Do(matchedStack.LdCount)
                .Ldc_I4_0()
                .Beq(settings.FAILURE.GetRef())
                .Br(settings.SUCCESS.GetRef());
        }
コード例 #5
0
        public void Build(EmitSyntax emit)
        {
            // Debug.WriteLine("DFA for " + descriptor.Name + ":");
            // Debug.WriteLine(dfa);
            int failState = dfa.StateCount;

            var locals = emit.Locals;
            var labels = emit.Labels;

            var FIN     = labels.Generate();
            var current = locals.Generate("ch");

            var Lgoto = new Ref <Labels> [dfa.StateCount + 1]; // label for incrementing ch position
            var Mgoto = new Ref <Labels> [dfa.StateCount];     // assing ch using current position
            var Tgoto = new Ref <Labels> [dfa.StateCount];     // transition dispatching switch-code

            for (int i = 0; i != dfa.StateCount; ++i)
            {
                Lgoto[i] = labels.Generate().GetRef();
                Mgoto[i] = labels.Generate().GetRef();
                Tgoto[i] = labels.Generate().GetRef();
            }

            Lgoto[failState] = FIN.GetRef();

            emit
            .Local(current, emit.Types.Char)
            .Ldarg(0).Ldfld((ScanCursor c) => c.InnerState)
            .Switch(Mgoto)
            .Br(Lgoto[0])
            ;

            foreach (var S in dfa.EnumerateStates())
            {
                int state = S.Index;

                emit
                .Label(Lgoto[state].Def);

                if (state != dfa.Start)
                {
                    // ++Cursor
                    emit
                    .Ldarg(0)     // for Stfld
                    .Ldarg(0).Ldfld((ScanCursor c) => c.Cursor)
                    .Ldc_I4_1()
                    .Add()
                    .Stfld((ScanCursor c) => c.Cursor)
                    ;
                }

                // If state is accepring then remember position
                // in Marker and save corresponding ActionId.
                if (S.IsAccepting)
                {
                    int actionCount = 0;
                    foreach (var action in S.Actions)
                    {
                        emit
                        .Ldarg(0)
                        .Ldfld((ScanCursor c) => c.Actions)
                        .Ldc_I4(actionCount++)
                        .Ldc_I4(action)
                        .Stelem_I4()
                        ;
                    }

                    emit
                    .Ldarg(0)
                    .Ldc_I4(actionCount)
                    .Stfld((ScanCursor c) => c.ActionCount)
                    ;

                    emit
                    .Ldarg(0)
                    .Ldc_I4(S.EnvelopeId)
                    .Stfld((ScanCursor c) => c.EnvelopeId)
                    ;

                    emit
                    .Ldarg(0)
                    .Ldarg(0).Ldfld((ScanCursor c) => c.Cursor)
                    .Stfld((ScanCursor c) => c.Marker);

                    // Save line/column information

                    emit
                    // cursor.MarkerLine = cursor.CursorLine;
                    .Ldarg(0)
                    .Ldarg(0)
                    .Ldfld((ScanCursor c) => c.CursorLine)
                    .Stfld((ScanCursor c) => c.MarkerLine)
                    // cursor.MarkerLineStart = cursor.CursorLineStart;
                    .Ldarg(0)
                    .Ldarg(0)
                    .Ldfld((ScanCursor c) => c.CursorLineStart)
                    .Stfld((ScanCursor c) => c.MarkerLineStart)
                    ;
                }

                if (S.IsNewline)
                {
                    emit
                    // ++cursor.CursorLine;
                    .Ldarg(0)
                    .Ldarg(0)
                    .Ldfld((ScanCursor c) => c.CursorLine)
                    .Ldc_I4_1()
                    .Add()
                    .Stfld((ScanCursor c) => c.CursorLine)
                    // cursor.CursorLineStart = cursor.Cursor;
                    .Ldarg(0)
                    .Ldarg(0)
                    .Ldfld((ScanCursor c) => c.Cursor)
                    .Stfld((ScanCursor c) => c.CursorLineStart)
                    ;
                }

                emit
                .Label(Mgoto[state].Def)
                // Get current input symbol
                .Ldarg(0).Ldfld((ScanCursor c) => c.Buffer)
                .Ldarg(0).Ldfld((ScanCursor c) => c.Cursor)
                .Ldelem_U2()
                .Stloc(current.GetRef())
                // Label for tunneling
                .Label(Tgoto[state].Def)
                ;

                if (S.IsFinal && state != 0)
                {
                    emit
                    .Br(FIN.GetRef());
                    continue;
                }

                int checkSentinelState = failState + 1;

                var stateRealTransitions = dfa.EnumerateRealTransitions(S).ToList();


                // Find custom sentinel transtion
                int customSentinelToState         = -1;
                var customSentinelTransitionIndex =
                    stateRealTransitions.FindIndex(pair => pair.Key.Contains(Sentinel));
                if (customSentinelTransitionIndex >= 0)
                {
                    customSentinelToState = stateRealTransitions[customSentinelTransitionIndex].Value;
                }

                // Sentinel check transiiton
                var sentinelTransition = new IntArrow <int>(Sentinel, checkSentinelState);
                stateRealTransitions.Insert(0, sentinelTransition);

                var generator = SwitchGenerator.Sparse(
                    stateRealTransitions.ToArray(),
                    failState,
                    UnicodeIntSetType.UnicodeInterval,
                    frequency: UnicodeFrequency.Default);
                generator.Build(
                    emit,
                    il => il.Ldloc(current.GetRef()),
                    (EmitSyntax il, int value) =>
                {
                    if (value == checkSentinelState)
                    {
                        var handleSentinelCharLabel =
                            (customSentinelToState >= 0)
                                    ? Lgoto[customSentinelToState]
                                    : FIN.GetRef();

                        // return for buffer filling if Cursor == Limit,
                        // otherwise transite on char
                        il
                        .Ldarg(0).Ldfld((ScanCursor c) => c.Limit)
                        .Ldarg(0).Ldfld((ScanCursor c) => c.Cursor)
                        .Bne_Un(handleSentinelCharLabel)

                        // Return for buffer filling
                        .Ldarg(0)
                        .Ldc_I4(state)
                        .Stfld((ScanCursor c) => c.InnerState)
                        .Ldc_I4(1)
                        .Ret()
                        ;
                    }
                    else if (value == failState && S.Tunnel >= 0)
                    {
                        // Tunnel-jump to other state and verify current
                        // input again in that state.
                        il.Br(Tgoto[S.Tunnel]);
                    }
                    else
                    {
                        // Transite to the valid or failed state
                        il.Br(Lgoto[value]);
                    }
                });
            }

            // Return to the last accepted position and report accept stage
            emit
            .Label(FIN)
            .Ldc_I4(0)
            .Ret()
            ;
        }
コード例 #6
0
        public void BuildBody(
            EmitSyntax emit,
            LanguageData data,
            Ref <Args> ruleId,
            Ref <Args> ruleArgs,
            Ref <Args> argsStart,
            Ref <Args> ctx,
            Ref <Args> lookbackStart)
        {
            Def <Labels> returnLabel = emit.Labels.Generate();

            var contextCode = new ContextCode(
                emit,
                il => il.Ldarg(ctx),
                il => il.Ldarg(lookbackStart),
                data,
                data.Grammar.GlobalContextProvider,
                data.LocalParseContexts);

            IActionCode code = new ProductionCode(
                emit,
                contextCode,
                ldRuleArgs:  il => il.Ldarg(ruleArgs),
                ldArgsStart: il => il.Ldarg(argsStart),
                returnLabel: returnLabel);

            var defaultLabel             = emit.Labels.Generate();
            var endWithSingleResultLabel = emit.Labels.Generate();
            var jumpTable = new Ref <Labels> [data.Grammar.Productions.Count];

            for (int i = 0; i != jumpTable.Length; ++i)
            {
                jumpTable[i] = emit.Labels.Generate().GetRef();
            }

            emit
            .Do(il => il.Ldarg(ruleId))
            .Switch(jumpTable)
            .Br(defaultLabel.GetRef());

            foreach (var prod in data.Grammar.Productions)
            {
                emit.Label(jumpTable[prod.Index].Def);

                if (0 == prod.Actions.Count)
                {
                    // Augumented start rule has null action and should never be invoked.
                    // Also it is possible that for some platforms production may have default
                    // action.
                    emit.Ldnull();
                }
                else
                {
                    foreach (var action in prod.Actions)
                    {
                        code = GenerateActionCode(code, action);
                    }
                }

                emit.Br(endWithSingleResultLabel.GetRef());
            }

            emit
            .Label(defaultLabel)
            .Ldnull()
            .Label(endWithSingleResultLabel)
            .Label(returnLabel)
            .Ret();
        }
コード例 #7
0
        public EmitSyntax EmitLoopPass(EmitSyntax emit, bool loop)
        {
            emit.Label(START);

            FetchNext(emit, END);
            body(emit);

            if (loop)
            {
                emit.Br(START.GetRef());
            }

            return emit
                .Label(END)
                .Nop();
        }
コード例 #8
0
        public void BuildBody(
            EmitSyntax emit, 
            LanguageData data,
            Ref<Args> ruleId,
            Ref<Args> ruleArgs,
            Ref<Args> argsStart,
            Ref<Args> ctx,
            Ref<Args> lookbackStart)
        {
            Def<Labels> returnLabel = emit.Labels.Generate();

            var contextCode = new ContextCode(
                emit,
                il => il.Ldarg(ctx),
                il => il.Ldarg(lookbackStart),
                data,
                data.Grammar.GlobalContextProvider,
                data.LocalParseContexts);

            IActionCode code = new ProductionCode(
                emit,
                contextCode,
                ldRuleArgs:  il => il.Ldarg(ruleArgs),
                ldArgsStart: il => il.Ldarg(argsStart),
                returnLabel: returnLabel);

            var defaultLabel = emit.Labels.Generate();
            var endWithSingleResultLabel = emit.Labels.Generate();
            var jumpTable = new Ref<Labels>[data.Grammar.Productions.Count];
            for (int i = 0; i != jumpTable.Length; ++i)
            {
                jumpTable[i] = emit.Labels.Generate().GetRef();
            }

            emit
                .Do(il => il.Ldarg(ruleId))
                .Switch(jumpTable)
                .Br(defaultLabel.GetRef());

            foreach (var prod in data.Grammar.Productions)
            {
                emit.Label(jumpTable[prod.Index].Def);

                if (0 == prod.Actions.Count)
                {
                    // Augumented start rule has null action and should never be invoked.
                    // Also it is possible that for some platforms production may have default
                    // action.
                    emit.Ldnull();
                }
                else
                {
                    foreach (var action in prod.Actions)
                    {
                        code = GenerateActionCode(code, action);
                    }
                }

                emit.Br(endWithSingleResultLabel.GetRef());
            }

            emit
                .Label(defaultLabel)
                .Ldnull()
                .Label(endWithSingleResultLabel)
                .Label(returnLabel)
                .Ret();
        }
コード例 #9
0
 public IActionCode ReturnFromAction()
 {
     emit.Br(RETURN);
     return(this);
 }
コード例 #10
0
 public void EmitReturn()
 {
     emit.Br(ReturnLabel.GetRef());
 }
コード例 #11
0
        public ILNfaCompiler(AstNode node, EmitSyntax emit, ILCompilerSettings settings)
        {
            this.Scanner = new StemScanner();

            this.emit     = emit;
            this.settings = settings;

            var labels = emit.Labels;

            NEXT_THREAD     = labels.Generate();
            RESUME_ALL      = labels.Generate();
            NEXT_INPUT      = labels.Generate();
            THREAD_SUSPEND  = labels.Generate();
            THREAD_DISPATCH = labels.Generate();

            var LABEL0               = labels.Generate();
            var LABEL1               = labels.Generate();
            var POSTCOMPILEINIT      = labels.Generate();
            var POSTCOMPILEINIT_BACK = labels.Generate();

            var locals = emit.Locals;

            slots = locals.Generate();
            labelIndexToLocation = locals.Generate();
            intTmp    = locals.Generate();
            threadTmp = locals.Generate();


            INfaVM code = this;

            this.inputPump = new ArrayLoopGenerator(
                valueType: emit.Types.Int32,
                ldarray:   settings.LdInput,
                body:
                il => il
                .Do(matchedStack.Clear)
                .Br(RESUME_ALL.GetRef())
                );

            this.runningStack   = new StackGenerator(emit, typeof(Thread));
            this.suspendedStack = new StackGenerator(emit, typeof(Thread));
            this.matchedStack   = new StackGenerator(emit, typeof(Thread));
            this.tmpStack       = new StackGenerator(emit, typeof(Thread), nullContainer: true);

            emit
            .Local(intTmp, emit.Types.Int32)
            .Local(slots, emit.Types.Import(typeof(int[])))
            .Local(labelIndexToLocation, emit.Types.Import(typeof(int[])))
            .Br(POSTCOMPILEINIT.GetRef())
            .Label(POSTCOMPILEINIT_BACK)
            .Local(threadTmp, emit.Types.Import(typeof(Thread)))
            .Ldloca(threadTmp.GetRef())
            .Initobj(emit.Types.Import(typeof(Thread)))
            .Do(inputPump.EmitInitialization)
            ;

            new RegularNfaVMCompiler().Compile(node, code);

            int LabelCount = Math.Max(1, indexToLabel.Count);

            this.runningStack.SetSize(LabelCount);
            this.suspendedStack.SetSize(LabelCount);
            this.matchedStack.SetSize(LabelCount);
            this.tmpStack.SetSize(LabelCount);

            emit
            .Label(POSTCOMPILEINIT)
            .Do(runningStack.Init)
            .Do(suspendedStack.Init)
            .Do(matchedStack.Init)
            .Do(tmpStack.Init)
            .Ldc_I4(slotCount)
            .Newarr(emit.Types.Int32)
            .Stloc(slots.GetRef())

            .Ldc_I4(LabelCount)
            .Newarr(emit.Types.Int32)
            .Stloc(labelIndexToLocation.GetRef())
            // Fill labelIndexToLocation with -1 :
            .Ldc_I4(LabelCount).Stloc(intTmp.GetRef())

            .Label(LABEL1)
            .Ldloc(intTmp.GetRef())
            .Ldc_I4_0()
            .Beq(LABEL0.GetRef())
            .Ldloc(intTmp.GetRef())
            .Ldc_I4(1)
            .Sub()
            .Stloc(intTmp.GetRef())
            .Ldloc(labelIndexToLocation.GetRef())
            .Ldloc(intTmp.GetRef())
            .Ldc_I4(int.MinValue)
            .Stelem_I4()
            .Br(LABEL1.GetRef())
            .Label(LABEL0)
            .Br(POSTCOMPILEINIT_BACK.GetRef());

            // Save thread as suspended (stack should already contain label index to suspend)
            emit.Label(THREAD_SUSPEND);
            StThreadValueByRuntimeLabelIndex();
            emit
            // Don't add thread if same thread (same label index
            // with current input location) already exists in a list.
            .Ldloc(labelIndexToLocation.GetRef())
            .Ldloca(threadTmp.GetRef())
            .Ldfld(LabelIndexField)
            .Ldelem_I4()
            .Ldloc(inputPump.Index.GetRef())
            .Beq(NEXT_THREAD.GetRef())

            // Mark label index as visited
            .Ldloc(labelIndexToLocation.GetRef())
            .Ldloca(threadTmp.GetRef())
            .Ldfld(LabelIndexField)
            .Ldloc(inputPump.Index.GetRef())
            .Stelem_I4()
            ;

            suspendedStack.PushFrom(emit, threadTmp);

            emit
            .Br(NEXT_THREAD.GetRef())

            .Label(RESUME_ALL)
            .Swap(suspendedStack.Stack, runningStack.Stack, tmpStack.Stack)
            .Swap(suspendedStack.Index, runningStack.Index, tmpStack.Index)
            .Label(NEXT_THREAD)
            ;

            runningStack
            .StackLoop(
                emit,
                (emit2, thread) =>
            {
                emit2
                .Ldloca(thread.GetRef())
                .Ldfld(LabelIndexField)
                .Label(THREAD_DISPATCH)
                .Switch(indexToLabel.Select(def => def.GetRef()).ToArray())
                .Br(settings.FAILURE.GetRef());
            })
            ;
            emit
            .Br(NEXT_INPUT.GetRef())
            ;

            emit
            .Label(NEXT_INPUT)
            ;

            inputPump.EmitLoopPass(emit, false);

            emit
            // Check if there are matched threads:
            .Do(matchedStack.LdCount)
            .Ldc_I4_0()
            .Beq(settings.FAILURE.GetRef())
            .Br(settings.SUCCESS.GetRef());
        }
コード例 #12
0
 public INfaVM Match()
 {
     matchedStack.PushFrom(emit, threadTmp);
     emit.Br(NEXT_THREAD.GetRef());
     return(this);
 }
コード例 #13
0
        public void Build(EmitSyntax emit)
        {
            // Debug.WriteLine("DFA for " + descriptor.Name + ":");
            // Debug.WriteLine(dfa);
            int failState  = dfa.StateCount;

            var locals = emit.Locals;
            var labels = emit.Labels;

            var FIN     = labels.Generate();
            var current = locals.Generate("ch");

            var Lgoto = new Ref<Labels>[dfa.StateCount + 1]; // label for incrementing ch position
            var Mgoto = new Ref<Labels>[dfa.StateCount];     // assing ch using current position
            var Tgoto = new Ref<Labels>[dfa.StateCount];     // transition dispatching switch-code

            for (int i = 0; i != dfa.StateCount; ++i)
            {
                Lgoto[i] = labels.Generate().GetRef();
                Mgoto[i] = labels.Generate().GetRef();
                Tgoto[i] = labels.Generate().GetRef();
            }

            Lgoto[failState] = FIN.GetRef();

            emit
                .Local(current, emit.Types.Char)
                .Ldarg(0) .Ldfld((ScanCursor c) => c.InnerState)
                .Switch(Mgoto)
                .Br(Lgoto[0])
                ;

            foreach (var S in dfa.EnumerateStates())
            {
                int state = S.Index;

                emit
                    .Label(Lgoto[state].Def);

                if (state != dfa.Start)
                {
                    // ++Cursor
                    emit
                        .Ldarg(0) // for Stfld
                        .Ldarg(0).Ldfld((ScanCursor c) => c.Cursor)
                        .Ldc_I4_1()
                        .Add()
                        .Stfld((ScanCursor c) => c.Cursor)
                    ;
                }

                // If state is accepring then remember position
                // in Marker and save corresponding ActionId.
                if (S.IsAccepting)
                {
                    int actionCount = 0;
                    foreach (var action in S.Actions)
                    {
                        emit
                            .Ldarg(0)
                            .Ldfld((ScanCursor c) => c.Actions)
                            .Ldc_I4(actionCount++)
                            .Ldc_I4(action)
                            .Stelem_I4()
                            ;
                    }

                    emit
                        .Ldarg(0)
                        .Ldc_I4(actionCount)
                        .Stfld((ScanCursor c) => c.ActionCount)
                        ;

                    emit
                        .Ldarg(0)
                        .Ldc_I4(S.EnvelopeId)
                        .Stfld((ScanCursor c) => c.EnvelopeId)
                        ;

                    emit
                        .Ldarg(0)
                        .Ldarg(0) .Ldfld((ScanCursor c) => c.Cursor)
                        .Stfld((ScanCursor c) => c.Marker);

                    // Save line/column information

                    emit
                        // cursor.MarkerLine = cursor.CursorLine;
                        .Ldarg(0)
                        .Ldarg(0)
                        .Ldfld((ScanCursor c) => c.CursorLine)
                        .Stfld((ScanCursor c) => c.MarkerLine)
                        // cursor.MarkerLineStart = cursor.CursorLineStart;
                        .Ldarg(0)
                        .Ldarg(0)
                        .Ldfld((ScanCursor c) => c.CursorLineStart)
                        .Stfld((ScanCursor c) => c.MarkerLineStart)
                        ;
                }

                if (S.IsNewline)
                {
                    emit
                        // ++cursor.CursorLine;
                        .Ldarg(0)
                        .Ldarg(0)
                        .Ldfld((ScanCursor c) => c.CursorLine)
                        .Ldc_I4_1()
                        .Add()
                        .Stfld((ScanCursor c) => c.CursorLine)
                        // cursor.CursorLineStart = cursor.Cursor;
                        .Ldarg(0)
                        .Ldarg(0)
                        .Ldfld((ScanCursor c) => c.Cursor)
                        .Stfld((ScanCursor c) => c.CursorLineStart)
                        ;
                }

                emit
                    .Label(Mgoto[state].Def)
                // Get current input symbol
                    .Ldarg(0) .Ldfld((ScanCursor c) => c.Buffer)
                    .Ldarg(0) .Ldfld((ScanCursor c) => c.Cursor)
                    .Ldelem_U2()
                    .Stloc(current.GetRef())
                // Label for tunneling
                    .Label(Tgoto[state].Def)
                    ;

                if (S.IsFinal && state != 0)
                {
                    emit
                        .Br(FIN.GetRef());
                    continue;
                }

                int checkSentinelState = failState + 1;

                var stateRealTransitions = dfa.EnumerateRealTransitions(S).ToList();

                // Find custom sentinel transtion
                int customSentinelToState = -1;
                var customSentinelTransitionIndex =
                    stateRealTransitions.FindIndex(pair => pair.Key.Contains(Sentinel));
                if (customSentinelTransitionIndex >= 0)
                {
                    customSentinelToState = stateRealTransitions[customSentinelTransitionIndex].Value;
                }

                // Sentinel check transiiton
                var sentinelTransition = new IntArrow<int>(Sentinel, checkSentinelState);
                stateRealTransitions.Insert(0, sentinelTransition);

                var generator = SwitchGenerator.Sparse(
                                    stateRealTransitions.ToArray(),
                                    failState,
                                    UnicodeIntSetType.UnicodeInterval,
                                    frequency: UnicodeFrequency.Default);
                generator.Build(
                    emit,
                    il => il.Ldloc(current.GetRef()),
                    (EmitSyntax il, int value) =>
                    {
                        if (value == checkSentinelState)
                        {
                            var handleSentinelCharLabel =
                                    (customSentinelToState >= 0)
                                    ? Lgoto[customSentinelToState]
                                    : FIN.GetRef();

                            // return for buffer filling if Cursor == Limit,
                            // otherwise transite on char
                            il
                                .Ldarg(0) .Ldfld((ScanCursor c) => c.Limit)
                                .Ldarg(0) .Ldfld((ScanCursor c) => c.Cursor)
                                .Bne_Un(handleSentinelCharLabel)

                                // Return for buffer filling
                                .Ldarg(0)
                                .Ldc_I4(state)
                                .Stfld((ScanCursor c) => c.InnerState)
                                .Ldc_I4(1)
                                .Ret()
                                ;
                        }
                        else if (value == failState && S.Tunnel >= 0)
                        {
                            // Tunnel-jump to other state and verify current
                            // input again in that state.
                            il.Br(Tgoto[S.Tunnel]);
                        }
                        else
                        {
                            // Transite to the valid or failed state
                            il.Br(Lgoto[value]);
                        }
                    });
            }

            // Return to the last accepted position and report accept stage
            emit
                .Label(FIN)
                .Ldc_I4(0)
                .Ret()
                ;
        }
コード例 #14
0
        public EmitSyntax StackLoop(EmitSyntax emit, Action<EmitSyntax, Def<Locals>> emitLoopBody)
        {
            var labels = emit.Labels;
            var START = labels.Generate();
            var END = labels.Generate();

            var item = emit.Locals.Generate();

            emit
                .Local(item, itemType)
                .Label(START)
                .Ldloc(index.GetRef())
                .Ldc_I4(0)
                .Beq(END.GetRef())
                .Do(Pop)
                .Stloc(item.GetRef())
                ;

            emitLoopBody(emit, item);

            emit
                .Br(START.GetRef())
                .Label(END)
                .Nop();

            return emit;
        }