Example #1
0
 public ChangeAlignTo(AlignTo alignTo, AlignToHandler alignToHandler)
 {
     this.alignTo        = alignTo;
     this.alignToHandler = alignToHandler;
 }
Example #2
0
 public abstract void SetAlignTo(AlignTo alignTo);
Example #3
0
 public static Icon GetIconForAlign(AlignTo align)
 {
     return(IconPool.GetIcon($"Tools.{Enum.GetName(typeof(AlignTo), align)}"));
 }
Example #4
0
 public override void SetAlignTo(AlignTo alignTo)
 {
     CenterAlignTo = alignTo;
     SetTextAndTexture();
 }
Example #5
0
        /// <summary>
        /// Lowers an align-view-to operation.
        /// </summary>
        private static void Lower(
            RewriterContext context,
            TypeLowering <ViewType> typeLowering,
            AlignTo value)
        {
            var builder  = context.Builder;
            var location = value.Location;

            // Extract basic view information from the converted structure
            var pointer = builder.CreateGetField(
                location,
                value.Source,
                new FieldSpan(0));
            var length = builder.CreateGetField(
                location,
                value.Source,
                new FieldSpan(1));

            // Build the final result structure instance
            var resultBuilder = builder.CreateDynamicStructure(location);

            // Convert the current input pointer to a 64-bit integer value
            var pointerAsInt = builder.CreatePointerAsIntCast(
                location,
                pointer,
                BasicValueType.Int64);

            // Compute the aligned pointer and convert it to an integer value
            var aligned = builder.CreateAlignTo(
                location,
                pointer,
                value.AlignmentInBytes);
            var alignedAsInt = builder.CreatePointerAsIntCast(
                location,
                aligned,
                BasicValueType.Int64);

            // Compute the number of elements to skip:
            // Min((aligned - ptr) / SizeOf(ElementType), length)
            var viewType       = typeLowering[value].As <ViewType>(location);
            var elementsToSkip = builder.CreateArithmetic(
                location,
                builder.CreateArithmetic(
                    location,
                    builder.CreateArithmetic(
                        location,
                        alignedAsInt,
                        pointerAsInt,
                        BinaryArithmeticKind.Sub),
                    builder.CreateSizeOf(location, viewType.ElementType),
                    BinaryArithmeticKind.Div),
                length,
                BinaryArithmeticKind.Min);

            // Create the prefix view that starts at the original pointer offset and
            // includes elementsToSkip many elements.
            {
                resultBuilder.Add(pointer);
                resultBuilder.Add(elementsToSkip);
            }

            // Create the main view that starts at the aligned pointer offset and has a
            // length of remainingLength
            {
                resultBuilder.Add(aligned);
                var remainingLength = builder.CreateArithmetic(
                    location,
                    length,
                    elementsToSkip,
                    BinaryArithmeticKind.Sub);
                resultBuilder.Add(remainingLength);
            }

            var result = resultBuilder.Seal();

            context.ReplaceAndRemove(value, result);
        }
Example #6
0
        /// <summary cref="IBackendCodeGenerator.GenerateCode(AlignTo)"/>
        public void GenerateCode(AlignTo value)
        {
            // Load the base view pointer
            var source                   = Load(value.Source);
            var target                   = Allocate(value);
            var alignmentVariable        = Load(value.AlignmentInBytes);
            var arithmeticBasicValueType =
                value.Source.BasicValueType.GetArithmeticBasicValueType(true);

            // var baseOffset = (int)ptr & (alignmentInBytes - 1);
            var baseOffset = AllocateType(arithmeticBasicValueType);

            using (var statement = BeginStatement(baseOffset))
            {
                statement.AppendCast(arithmeticBasicValueType);
                statement.AppendArgument(source);
                statement.AppendCommand(CLInstructions.GetArithmeticOperation(
                                            BinaryArithmeticKind.And,
                                            isFloat: false,
                                            out bool _));
                // Optimize for the case in which the alignment is a constant value
                if (value.TryGetAlignmentConstant(out int alignment))
                {
                    statement.AppendConstant(alignment - 1);
                }
                else
                {
                    statement.AppendCommand('(');
                    statement.AppendArgument(alignmentVariable);
                    statement.AppendCommand(CLInstructions.GetArithmeticOperation(
                                                BinaryArithmeticKind.Sub,
                                                isFloat: false,
                                                out bool _));
                    statement.AppendConstant(1);
                    statement.AppendCommand(')');
                }
            }

            // if (baseOffset == 0) { 0 } else { alignment - baseOffset }
            var adjustment = AllocateType(arithmeticBasicValueType);

            using (var selectStatement = BeginStatement(adjustment))
            {
                selectStatement.AppendArgument(baseOffset);
                selectStatement.AppendCommand(" == 0 ? 0 : (");
                if (value.TryGetAlignmentConstant(out int alignmentConstant))
                {
                    selectStatement.AppendConstant(alignmentConstant);
                }
                else
                {
                    selectStatement.AppendArgument(alignmentVariable);
                }
                selectStatement.AppendCommand(CLInstructions.GetArithmeticOperation(
                                                  BinaryArithmeticKind.Sub,
                                                  isFloat: false,
                                                  out bool _));
                selectStatement.AppendArgument(baseOffset);
                selectStatement.AppendCommand(')');
            }

            // Adjust the given pointer
            using var command = BeginStatement(target);
            command.AppendCast(value.Type);
            command.AppendCommand('(');
            command.AppendCast(arithmeticBasicValueType);
            command.AppendArgument(source);
            command.AppendCommand(CLInstructions.GetArithmeticOperation(
                                      BinaryArithmeticKind.Add,
                                      isFloat: false,
                                      out bool _));
            command.AppendArgument(adjustment);
            command.AppendCommand(')');
        }