Example #1
0
        private void EmitRangeCheckedBranch(ConstantValue startConstant, ConstantValue endConstant, object targetLabel)
        {
            _builder.EmitLoad(_key);

            // Normalize the key to 0 if needed

            // Emit:    ldc constant
            //          sub
            if (!startConstant.IsDefaultValue)
            {
                _builder.EmitConstantValue(startConstant);
                _builder.EmitOpCode(ILOpCode.Sub);
            }

            if (_keyTypeCode.Is64BitIntegral())
            {
                _builder.EmitLongConstant(endConstant.Int64Value - startConstant.Int64Value);
            }
            else
            {
                _builder.EmitIntConstant(endConstant.Int32Value - startConstant.Int32Value);
            }

            _builder.EmitBranch(ILOpCode.Ble_un, targetLabel, ILOpCode.Bgt_un);
        }
Example #2
0
        private void EmitRangeCheckedBranch(
            ConstantValue startConstant,
            ConstantValue endConstant,
            object targetLabel
            )
        {
            _builder.EmitLoad(_key);

            // Normalize the key to 0 if needed

            // Emit:    ldc constant
            //          sub
            if (!startConstant.IsDefaultValue)
            {
                _builder.EmitConstantValue(startConstant);
                _builder.EmitOpCode(ILOpCode.Sub);
            }

            if (_keyTypeCode.Is64BitIntegral())
            {
                _builder.EmitLongConstant(endConstant.Int64Value - startConstant.Int64Value);
            }
            else
            {
                int Int32Value(ConstantValue value)
                {
                    // ConstantValue does not correctly convert byte and ushort values to int.
                    // It sign extends them rather than padding them. We compensate for that here.
                    // See also https://github.com/dotnet/roslyn/issues/18579
                    switch (value.Discriminator)
                    {
                    case ConstantValueTypeDiscriminator.Byte:
                        return(value.ByteValue);

                    case ConstantValueTypeDiscriminator.UInt16:
                        return(value.UInt16Value);

                    default:
                        return(value.Int32Value);
                    }
                }

                _builder.EmitIntConstant(Int32Value(endConstant) - Int32Value(startConstant));
            }

            _builder.EmitBranch(ILOpCode.Ble_un, targetLabel, ILOpCode.Bgt_un);
        }
Example #3
0
        private void EmitNormalizedSwitchKey(ConstantValue startConstant, ConstantValue endConstant, object bucketFallThroughLabel)
        {
            _builder.EmitLoad(_key);

            // Normalize the key to 0 if needed

            // Emit:    ldc constant
            //          sub
            if (!startConstant.IsDefaultValue)
            {
                _builder.EmitConstantValue(startConstant);
                _builder.EmitOpCode(ILOpCode.Sub);
            }

            // range-check normalized value if needed
            EmitRangeCheckIfNeeded(startConstant, endConstant, bucketFallThroughLabel);

            // truncate key to 32bit
            _builder.EmitNumericConversion(_keyTypeCode, Microsoft.Cci.PrimitiveTypeCode.UInt32, false);
        }
 public void EmitLoadTarget(ILBuilder il)
 {
     this.Place.EmitLoad(il);
     il.EmitOpCode(ILOpCode.Ldfld);
     il.EmitSymbolToken(_factory._cg.Module, _factory._cg.Diagnostics, _target, null);
 }
Example #5
0
        public void EmitStore(ILBuilder il)
        {
            //if (_property.Setter == null)
            //    throw new InvalidOperationException();

            var stack = 0;
            var setter = _property.SetMethod;

            if (_holder != null)
            {
                stack -= 1;
            }

            //
            il.EmitOpCode(setter.IsVirtual ? ILOpCode.Callvirt : ILOpCode.Call, stack);
            il.EmitToken(setter, null, DiagnosticBag.GetInstance());

            //
            Debug.Assert(setter.ReturnType.SpecialType == SpecialType.System_Void);
        }
Example #6
0
        public TypeSymbol EmitLoad(ILBuilder il)
        {
            //if (_property.Getter == null)
            //    throw new InvalidOperationException();

            var stack = +1;
            var getter = _property.GetMethod;

            if (_holder != null)
            {
                Debug.Assert(!getter.IsStatic);
                _holder.EmitLoad(il);   // {holder}
                stack -= 1;
            }

            il.EmitOpCode(getter.IsVirtual ? ILOpCode.Callvirt : ILOpCode.Call, stack);
            il.EmitToken(getter, null, DiagnosticBag.GetInstance());

            //
            return getter.ReturnType;
        }
Example #7
0
 void EmitOpCode(ILBuilder il, ILOpCode code)
 {
     il.EmitOpCode(code);
     il.EmitToken(_field, null, DiagnosticBag.GetInstance());    // .{field}
 }
        public static TypeSymbol EmitConvertToPhpValue(TypeSymbol from, TypeRefMask fromHint, ILBuilder il, Emit.PEModuleBuilder module, DiagnosticBag diagnostic)
        {
            Contract.ThrowIfNull(from);

            var compilation = module.Compilation;

            switch (from.SpecialType)
            {
                case SpecialType.System_Boolean:
                    il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_Boolean);
                    break;
                case SpecialType.System_Int32:
                    il.EmitOpCode(ILOpCode.Conv_i8);   // Int32 -> Int64
                    goto case SpecialType.System_Int64; // PhpValue.Create((long)<stack>)
                case SpecialType.System_Int64:
                    il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_Long);
                    break;
                case SpecialType.System_Double:
                    il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_Double);
                    break;
                case SpecialType.System_Void:
                    Emit_PhpValue_Void(il, module, diagnostic);
                    break;
                default:
                    if (from == compilation.CoreTypes.PhpAlias)
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_PhpAlias)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else if (from == compilation.CoreTypes.PhpValue)
                    {
                        // nop
                        break;
                    }
                    else if (from == compilation.CoreTypes.String)
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_String)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else if (from == compilation.CoreTypes.PhpString)
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_PhpString)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else if (from == compilation.CoreTypes.PhpNumber)
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_PhpNumber)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else if (from.IsOfType(compilation.CoreTypes.PhpArray))
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_PhpArray)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else if (from == compilation.CoreTypes.IntStringKey)
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.Create_IntStringKey)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else if (from.IsReferenceType)
                    {
                        il.EmitCall(module, diagnostic, ILOpCode.Call, compilation.CoreMethods.PhpValue.FromClass_Object)
                            .Expect(compilation.CoreTypes.PhpValue);
                        break;
                    }
                    else
                    {
                        throw new NotImplementedException($"{from.Name}");
                    }
            }

            //
            return compilation.CoreTypes.PhpValue;
        }
Example #9
0
 /// <summary>
 /// Emits load of PhpValue representing void.
 /// </summary>
 static TypeSymbol Emit_PhpValue_Void(ILBuilder il, Emit.PEModuleBuilder module, DiagnosticBag diagnostic)
 {
     il.EmitOpCode(ILOpCode.Ldsfld);
     il.EmitSymbolToken(module, diagnostic, module.Compilation.CoreMethods.PhpValue.Void, null);
     return module.Compilation.CoreTypes.PhpValue;
 }