/// <summary cref="IValueVisitor.Visit(SwitchBranch)"/>
        public void Visit(SwitchBranch branch)
        {
            var condition = Load(branch.Condition);

            AppendIndent();
            Builder.Append("switch (");
            Builder.Append(condition.ToString());
            Builder.AppendLine(")");

            AppendIndent();
            Builder.AppendLine("{");

            for (int i = 0, e = branch.NumCasesWithoutDefault; i < e; ++i)
            {
                Builder.Append("case ");
                Builder.Append(i.ToString());
                Builder.AppendLine(":");
                PushAndAppendIndent();
                GotoStatement(branch.GetCaseTarget(i));
                PopIndent();
            }

            AppendIndent();
            Builder.AppendLine("default:");
            GotoStatement(branch.Targets[0]);
            Builder.AppendLine("}");
        }
Ejemplo n.º 2
0
        /// <summary cref="IBackendCodeGenerator.GenerateCode(SwitchBranch)"/>
        public void GenerateCode(SwitchBranch branch)
        {
            var condition = Load(branch.Condition);
            var indentStr = new string('\t', Indent);

            using var statement = BeginStatement($"switch ({condition}) {{\n");
            for (int i = 0, e = branch.NumCasesWithoutDefault; i < e; ++i)
            {
                statement.AppendOperation("{0}case {1}:\n{0}\t{2} {3};\n",
                                          indentStr,
                                          i,
                                          CLInstructions.GotoStatement,
                                          branch.GetCaseTarget(i));
            }
            statement.AppendOperation("{0}default:\n{0}\t{1} {2};\n{0}}}",
                                      indentStr,
                                      CLInstructions.GotoStatement,
                                      branch.Targets[0]);
        }
Ejemplo n.º 3
0
        /// <summary cref="IValueVisitor.Visit(SwitchBranch)"/>
        public void Visit(SwitchBranch branch)
        {
            var idx = LoadPrimitive(branch.Condition);

            using (var lowerBoundsScope = new PredicateScope(this))
            {
                // Emit less than
                var lessThanCommand = PTXInstructions.GetCompareOperation(
                    CompareKind.LessThan,
                    ArithmeticBasicValueType.Int32);
                using (var command = BeginCommand(
                           lessThanCommand))
                {
                    command.AppendArgument(lowerBoundsScope.PredicateRegister);
                    command.AppendArgument(idx);
                    command.AppendConstant(0);
                }

                using (var upperBoundsScope = new PredicateScope(this))
                {
                    using (var command = BeginCommand(
                               PTXInstructions.BranchIndexRangeComparison))
                    {
                        command.AppendArgument(upperBoundsScope.PredicateRegister);
                        command.AppendArgument(idx);
                        command.AppendConstant(branch.NumCasesWithoutDefault);
                        command.AppendArgument(lowerBoundsScope.PredicateRegister);
                    }
                    using (var command = BeginCommand(
                               PTXInstructions.BranchOperation,
                               new PredicateConfiguration(upperBoundsScope.PredicateRegister, true)))
                    {
                        var defaultTarget = blockLookup[branch.DefaultBlock];
                        command.AppendLabel(defaultTarget);
                    }
                }
            }

            var targetLabel = DeclareLabel();

            MarkLabel(targetLabel);
            Builder.Append('\t');
            Builder.Append(PTXInstructions.BranchTargetsDeclaration);
            Builder.Append(' ');
            for (int i = 0, e = branch.NumCasesWithoutDefault; i < e; ++i)
            {
                var caseTarget = branch.GetCaseTarget(i);
                var caseLabel  = blockLookup[caseTarget];
                Builder.Append(caseLabel);
                if (i + 1 < e)
                {
                    Builder.Append(", ");
                }
            }
            Builder.AppendLine(";");

            using (var command = BeginCommand(
                       PTXInstructions.BranchIndexOperation))
            {
                command.AppendArgument(idx);
                command.AppendLabel(targetLabel);
            }
        }