📄 emitter.vb
字号:
End Sub Shared Sub EmitConv_U8_Overflow(ByVal Info As EmitInfo, ByVal FromType As Type) FromType = Helper.GetTypeOrTypeBuilder(FromType) Info.Stack.Pop(FromType) Info.ILGen.Emit(OpCodes.Conv_Ovf_U8) Info.Stack.Push(Info.Compiler.TypeCache.System_UInt64) End Sub Shared Sub EmitConv_U8_Overflow_Underflow(ByVal Info As EmitInfo, ByVal FromType As Type) FromType = Helper.GetTypeOrTypeBuilder(FromType) Info.Stack.Pop(FromType) Info.ILGen.Emit(OpCodes.Conv_Ovf_U8_Un) Info.Stack.Push(Info.Compiler.TypeCache.System_UInt64) End Sub Shared Sub EmitConv_I8(ByVal Info As EmitInfo, ByVal FromType As Type) FromType = Helper.GetTypeOrTypeBuilder(FromType) Info.Stack.Pop(FromType) Info.ILGen.Emit(OpCodes.Conv_I8) Info.Stack.Push(Info.Compiler.TypeCache.System_Int64) End Sub Shared Sub EmitConv_I8_Overflow(ByVal Info As EmitInfo, ByVal FromType As Type) FromType = Helper.GetTypeOrTypeBuilder(FromType) Info.Stack.Pop(FromType) Info.ILGen.Emit(OpCodes.Conv_Ovf_I8) Info.Stack.Push(Info.Compiler.TypeCache.System_Int64) End Sub Shared Sub EmitConv_I8_Overflow_Underflow(ByVal Info As EmitInfo, ByVal FromType As Type) FromType = Helper.GetTypeOrTypeBuilder(FromType) Info.Stack.Pop(FromType) Info.ILGen.Emit(OpCodes.Conv_Ovf_I8_Un) Info.Stack.Push(Info.Compiler.TypeCache.System_Int64) End Sub Shared Sub EmitConv_R8(ByVal Info As EmitInfo, ByVal FromType As Type) FromType = Helper.GetTypeOrTypeBuilder(FromType) Info.Stack.Pop(FromType) Info.ILGen.Emit(OpCodes.Conv_R8) Info.Stack.Push(Info.Compiler.TypeCache.System_Double) End Sub Shared Sub EmitConv_R4(ByVal Info As EmitInfo, ByVal FromType As Type) FromType = Helper.GetTypeOrTypeBuilder(FromType) Info.Stack.Pop(FromType) Info.ILGen.Emit(OpCodes.Conv_R4) Info.Stack.Push(Info.Compiler.TypeCache.System_Single) End Sub ''' <summary> ''' Convert the value on the stack to ToType. ''' </summary> ''' <param name="ToType"></param> ''' <remarks></remarks> Shared Sub EmitConversion(ByVal FromType As Type, ByVal ToType As Type, ByVal Info As EmitInfo) Dim ToTypeOriginal, FromTypeOriginal As Type If Helper.CompareType(FromType, Info.Compiler.TypeCache.Nothing) Then Return ToTypeOriginal = ToType ToType = Helper.GetTypeOrTypeBuilder(ToType) 'Dim FromType As Type = Info.Stack.Peek Dim FromTP, ToTP As TypeCode Dim converted As Boolean = False FromTypeOriginal = FromType FromType = Helper.GetTypeOrTypeBuilder(FromType) FromTP = Helper.GetTypeCode(Info.Compiler, FromType) ToTP = Helper.GetTypeCode(info.Compiler, ToType)#If DEBUGIMPLICITCONVERSION Then If FromTP <> ToTP OrElse FromTP = TypeCode.Object OrElse ToTP = TypeCode.Object Then Compiler.Report.WriteLine(vbnc.Report.ReportLevels.Debug, String.Format("Converting from '{0}' to '{1}'.", FromTP.ToString, ToTP.ToString)) End If#End If If ToTP = TypeCode.Object AndAlso FromTP <> TypeCode.Object AndAlso Helper.CompareType(ToType, Info.Compiler.TypeCache.System_Object) = False Then EmitValueTypeToObjectConversion(Info, FromType, ToType) Return End If Dim Switch As TypeCombinations = Helper.GetCombination(FromTP, ToTP) Select Case Switch Case TypeCombinations.Boolean_Boolean, _ TypeCombinations.Byte_Byte, _ TypeCombinations.Char_Char, _ TypeCombinations.DateTime_DateTime, _ TypeCombinations.Decimal_Decimal, _ TypeCombinations.Double_Double, _ TypeCombinations.Empty_Empty, _ TypeCombinations.Int16_Int16, _ TypeCombinations.Int32_Int32, _ TypeCombinations.Int64_Int64, _ TypeCombinations.SByte_SByte, _ TypeCombinations.Single_Single, _ TypeCombinations.String_String, _ TypeCombinations.UInt16_UInt16, _ TypeCombinations.UInt32_UInt32, _ TypeCombinations.UInt64_UInt64 Return 'Nothing to convert, both types are equal Case TypeCombinations.Object_Object Dim tmpTo, tmpFrom As Type If Info.IsExplicitConversion = False Then 'If TypeOf FromType Is TypeDescriptor Then ' tmpFrom = DirectCast(FromType, TypeDescriptor).TypeInReflection 'Else ' tmpFrom = FromType 'End If tmpFrom = FromTypeOriginal 'If TypeOf ToTypeIn Is TypeDescriptor Then ' tmpTo = DirectCast(ToType, TypeDescriptor).TypeInReflection 'Else tmpTo = ToTypeOriginal ' End If If Helper.CompareType(tmpTo, tmpFrom) Then converted = True ElseIf Helper.IsAssignable(Info.Compiler, tmpFrom, tmpTo) Then converted = True Else converted = True Info.Compiler.Report.WriteLine(Report.ReportLevels.Debug, "The compiler supposes a conversion from " & tmpFrom.Name & " to " & tmpTo.Name & " is valid.") End If Else Info.ILGen.Emit(OpCodes.Castclass, ToType) 'Emitter.EmitCastClass(Info, FromType, ToType) converted = True End If ' ToSByte conversions Case TypeCombinations.Byte_SByte If Info.IsExplicitConversion Then If Info.IntegerOverflowChecks Then Info.ILGen.Emit(OpCodes.Conv_Ovf_I1_Un) : converted = True Else Info.ILGen.Emit(OpCodes.Conv_I1) End If Else Helper.Stop() End If Case TypeCombinations.Int16_SByte Case TypeCombinations.UInt16_SByte Case TypeCombinations.Int32_SByte If Info.IsExplicitConversion Then If Info.IntegerOverflowChecks Then Info.ILGen.Emit(OpCodes.Conv_Ovf_I1) : converted = True Else Info.ILGen.Emit(OpCodes.Conv_I1) : converted = True End If Else Helper.Stop() End If Case TypeCombinations.UInt32_SByte Case TypeCombinations.Int64_SByte Case TypeCombinations.UInt64_SByte Case TypeCombinations.Single_SByte Case TypeCombinations.Double_SByte Case TypeCombinations.Decimal_SByte 'ToByte conversions Case TypeCombinations.SByte_Byte, _ TypeCombinations.Int16_Byte, _ TypeCombinations.Int32_Byte, _ TypeCombinations.Int64_Byte If Info.IsExplicitConversion Then If Info.IntegerOverflowChecks Then Info.ILGen.Emit(OpCodes.Conv_Ovf_U1) : converted = True Else Info.ILGen.Emit(OpCodes.Conv_U1) : converted = True End If Else Helper.Stop() End If Case TypeCombinations.UInt16_Byte Case TypeCombinations.UInt32_Byte Case TypeCombinations.UInt64_Byte Case TypeCombinations.Single_Byte Case TypeCombinations.Double_Byte Case TypeCombinations.Decimal_Byte 'ToUInt64 conversions Case TypeCombinations.Byte_UInt16 Info.ILGen.Emit(OpCodes.Conv_U2) converted = True 'ToInt16 conversions Case TypeCombinations.Byte_Int16, _ TypeCombinations.SByte_Int16 Info.ILGen.Emit(OpCodes.Conv_I2) converted = True 'ToUInt32 conversions Case TypeCombinations.Byte_UInt32, _ TypeCombinations.UInt16_UInt32 Info.ILGen.Emit(OpCodes.Conv_U4) converted = True 'ToInt32 conversions Case TypeCombinations.SByte_Int32, _ TypeCombinations.Byte_Int32, _ TypeCombinations.UInt16_Int32, _ TypeCombinations.Int16_Int32 Info.ILGen.Emit(OpCodes.Conv_I4) converted = True Case TypeCombinations.Object_Int32 'Narrowing conversion If Info.IsExplicitConversion = False Then Helper.AddError() Else Emitter.EmitUnbox(Info, Info.Compiler.TypeCache.System_Int32) Emitter.EmitLdobj(Info, Info.Compiler.TypeCache.System_Int32) converted = True End If 'ToInt64 conversions Case TypeCombinations.Byte_Int64, _ TypeCombinations.SByte_Int64, _ TypeCombinations.UInt16_Int64, _ TypeCombinations.Int16_Int64, _ TypeCombinations.UInt32_Int64, _ TypeCombinations.Int32_Int64 Info.ILGen.Emit(OpCodes.Conv_I8) converted = True 'ToUInt64 conversions Case TypeCombinations.Byte_UInt64, _ TypeCombinations.UInt16_UInt64, _ TypeCombinations.UInt32_UInt64 Info.ILGen.Emit(OpCodes.Conv_U8) converted = True 'ToSingle conversions Case TypeCombinations.SByte_Single, _ TypeCombinations.Byte_Single, _ TypeCombinations.UInt16_Single, _ TypeCombinations.Int16_Single, _ TypeCombinations.UInt32_Single, _ TypeCombinations.Int32_Single, _ TypeCombinations.UInt64_Single, _ TypeCombinations.Int64_Single Info.ILGen.Emit(OpCodes.Conv_R4) converted = True Case TypeCombinations.Decimal_Single Info.ILGen.Emit(OpCodes.Call, Info.Compiler.TypeCache.System_Convert__ToSingle_Decimal) converted = True 'ToDouble conversions Case TypeCombinations.Decimal_Double Info.ILGen.Emit(OpCodes.Call, Info.Compiler.TypeCache.System_Convert__ToDouble_Decimal) converted = True Case TypeCombinations.SByte_Double, _ TypeCombinations.Byte_Double, _ TypeCombinations.UInt16_Double, _ TypeCombinations.Int16_Double, _ TypeCombinations.UInt32_Double, _ TypeCombinations.Int32_Double, _ TypeCombinations.UInt64_Double, _ TypeCombinations.Int64_Double, _ TypeCombinations.Single_Double Info.ILGen.Emit(OpCodes.Conv_R8) converted = True 'ToDecimal conversions Case TypeCombinations.Byte_Decimal, _ TypeCombinations.SByte_Decimal, _ TypeCombinations.Int16_Decimal Info.ILGen.Emit(OpCodes.Conv_I4) Info.ILGen.Emit(OpCodes.Newobj, Info.Compiler.TypeCache.System_Decimal__ctor_Int32) converted = True Case TypeCombinations.UInt16_Decimal, _ TypeCombinations.UInt32_Decimal Info.ILGen.Emit(OpCodes.Conv_I8) Info.ILGen.Emit(OpCodes.Newobj, Info.Compiler.TypeCache.System_Decimal__ctor_Int64) converted = True Case TypeCombinations.Int32_Decimal Info.ILGen.Emit(OpCodes.Newobj, Info.Compiler.TypeCache.System_Decimal__ctor_Int32) converted = True Case TypeCombinations.UInt64_Decimal Info.ILGen.Emit(OpCodes.Newobj, Info.Compiler.TypeCache.System_Decimal__ctor_UInt64) converted = True Case TypeCombinations.Int64_Decimal Info.ILGen.Emit(OpCodes.Newobj, Info.Compiler.TypeCache.System_Decimal__ctor_Int64) converted = True 'ToObject conversions Case TypeCombinations.SByte_Object Info.ILGen.Emit(OpCodes.Box, Info.Compiler.TypeCache.System_SByte) converted = True Case TypeCombinations.Byte_Object Info.ILGen.Emit(OpCodes.Box, Info.Compiler.TypeCache.System_Byte) converted = True Case TypeCombinations.Int16_Object Info.ILGen.Emit(OpCodes.Box, Info.Compiler.TypeCache.System_Int16) converted = True Case TypeCombinations.UInt16_Object Info.ILGen.Emit(OpCodes.Box, Info.Compiler.TypeCache.System_UInt16) converted = True Case TypeCombinations.Int32_Object Info.ILGen.Emit(OpCodes.Box, Info.Compiler.TypeCache.System_Int32) converted = True Case TypeCombinations.UInt32_Object Info.ILGen.Emit(OpCodes.Box, Info.Compiler.TypeCache.System_UInt32) converted = True Case TypeCombinations.Int64_Object Info.ILGen.Emit(OpCodes.Box, Info.Compiler.TypeCache.System_Int64) converted = True Case TypeCombinations.UInt64_Object Info.ILGen.Emit(OpCodes.Box, Info.Compiler.TypeCache.System_UInt64) converted = True Case TypeCombinations.Single_Object Info.ILGen.Emit(OpCodes.Box, Info.Compiler.TypeCache.System_Single) converted = True Case TypeCombinations.Double_Object Info.ILGen.Emit(OpCodes.Box, Info.Compiler.TypeCache.System_Double) converted = True Case TypeCombinations.String_Object converted = True Case TypeCombinations.Decimal_Object
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -