AILConv.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using ChocolArm64.State;
  2. using System;
  3. using System.Reflection;
  4. using System.Reflection.Emit;
  5. namespace ChocolArm64.Translation
  6. {
  7. static class AILConv
  8. {
  9. public static void EmitConv(AILEmitter Context, Type SrcType, Type TgtType)
  10. {
  11. if (SrcType == TgtType)
  12. {
  13. //If both types are equal we don't need to cast anything.
  14. return;
  15. }
  16. if (SrcType.IsPrimitive)
  17. {
  18. if (TgtType == typeof(byte))
  19. {
  20. Context.Generator.Emit(OpCodes.Conv_U1);
  21. }
  22. else if (TgtType == typeof(ushort))
  23. {
  24. Context.Generator.Emit(OpCodes.Conv_U2);
  25. }
  26. else if (TgtType == typeof(uint))
  27. {
  28. Context.Generator.Emit(OpCodes.Conv_U4);
  29. }
  30. else if (TgtType == typeof(ulong))
  31. {
  32. Context.Generator.Emit(OpCodes.Conv_U8);
  33. }
  34. else if (TgtType == typeof(float))
  35. {
  36. Context.Generator.Emit(OpCodes.Conv_R4);
  37. }
  38. else if (TgtType == typeof(double))
  39. {
  40. Context.Generator.Emit(OpCodes.Conv_R8);
  41. }
  42. else if (TgtType == typeof(AVec))
  43. {
  44. EmitMakeVec(Context, SrcType);
  45. }
  46. else
  47. {
  48. throw new ArgumentException(nameof(TgtType));
  49. }
  50. }
  51. else if (SrcType == typeof(AVec))
  52. {
  53. if (TgtType == typeof(float))
  54. {
  55. EmitScalarLdfld(Context, nameof(AVec.S0));
  56. }
  57. else if (TgtType == typeof(double))
  58. {
  59. EmitScalarLdfld(Context, nameof(AVec.D0));
  60. }
  61. else if (TgtType == typeof(byte))
  62. {
  63. EmitScalarLdfld(Context, nameof(AVec.B0));
  64. }
  65. else if (TgtType == typeof(ushort))
  66. {
  67. EmitScalarLdfld(Context, nameof(AVec.H0));
  68. }
  69. else if (TgtType == typeof(uint))
  70. {
  71. EmitScalarLdfld(Context, nameof(AVec.W0));
  72. }
  73. else if (TgtType == typeof(ulong))
  74. {
  75. EmitScalarLdfld(Context, nameof(AVec.X0));
  76. }
  77. else
  78. {
  79. throw new ArgumentException(nameof(TgtType));
  80. }
  81. }
  82. else
  83. {
  84. throw new ArgumentException(nameof(SrcType));
  85. }
  86. }
  87. private static void EmitScalarLdfld(AILEmitter Context,string FldName)
  88. {
  89. Context.Generator.Emit(OpCodes.Ldfld, typeof(AVec).GetField(FldName));
  90. }
  91. private static void EmitMakeVec(AILEmitter Context, Type SrcType)
  92. {
  93. string MthdName = nameof(MakeScalar);
  94. Type[] MthdTypes = new Type[] { SrcType };
  95. MethodInfo MthdInfo = typeof(AILConv).GetMethod(MthdName, MthdTypes);
  96. Context.Generator.Emit(OpCodes.Call, MthdInfo);
  97. }
  98. public static AVec MakeScalar(byte Value) => new AVec { B0 = Value };
  99. public static AVec MakeScalar(ushort Value) => new AVec { H0 = Value };
  100. public static AVec MakeScalar(uint Value) => new AVec { W0 = Value };
  101. public static AVec MakeScalar(float Value) => new AVec { S0 = Value };
  102. public static AVec MakeScalar(ulong Value) => new AVec { X0 = Value };
  103. public static AVec MakeScalar(double Value) => new AVec { D0 = Value };
  104. }
  105. }