InstEmitException.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System.Reflection.Emit;
  5. namespace ChocolArm64.Instructions
  6. {
  7. static partial class InstEmit
  8. {
  9. public static void Brk(ILEmitterCtx context)
  10. {
  11. EmitExceptionCall(context, nameof(CpuThreadState.OnBreak));
  12. }
  13. public static void Svc(ILEmitterCtx context)
  14. {
  15. EmitExceptionCall(context, nameof(CpuThreadState.OnSvcCall));
  16. }
  17. private static void EmitExceptionCall(ILEmitterCtx context, string mthdName)
  18. {
  19. OpCodeException64 op = (OpCodeException64)context.CurrOp;
  20. context.EmitStoreState();
  21. context.EmitLdarg(TranslatedSub.StateArgIdx);
  22. context.EmitLdc_I8(op.Position);
  23. context.EmitLdc_I4(op.Id);
  24. context.EmitPrivateCall(typeof(CpuThreadState), mthdName);
  25. //Check if the thread should still be running, if it isn't then we return 0
  26. //to force a return to the dispatcher and then exit the thread.
  27. context.EmitLdarg(TranslatedSub.StateArgIdx);
  28. context.EmitCallPropGet(typeof(CpuThreadState), nameof(CpuThreadState.Running));
  29. ILLabel lblEnd = new ILLabel();
  30. context.Emit(OpCodes.Brtrue_S, lblEnd);
  31. context.EmitLdc_I8(0);
  32. context.Emit(OpCodes.Ret);
  33. context.MarkLabel(lblEnd);
  34. if (context.CurrBlock.Next != null)
  35. {
  36. context.EmitLoadState();
  37. }
  38. else
  39. {
  40. context.EmitLdc_I8(op.Position + 4);
  41. context.Emit(OpCodes.Ret);
  42. }
  43. }
  44. public static void Und(ILEmitterCtx context)
  45. {
  46. OpCode64 op = context.CurrOp;
  47. context.EmitStoreState();
  48. context.EmitLdarg(TranslatedSub.StateArgIdx);
  49. context.EmitLdc_I8(op.Position);
  50. context.EmitLdc_I4(op.RawOpCode);
  51. context.EmitPrivateCall(typeof(CpuThreadState), nameof(CpuThreadState.OnUndefined));
  52. if (context.CurrBlock.Next != null)
  53. {
  54. context.EmitLoadState();
  55. }
  56. else
  57. {
  58. context.EmitLdc_I8(op.Position + 4);
  59. context.Emit(OpCodes.Ret);
  60. }
  61. }
  62. }
  63. }