AInstEmitException.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System.Reflection.Emit;
  5. namespace ChocolArm64.Instruction
  6. {
  7. static partial class AInstEmit
  8. {
  9. public static void Brk(AILEmitterCtx Context)
  10. {
  11. EmitExceptionCall(Context, nameof(AThreadState.OnBreak));
  12. }
  13. public static void Svc(AILEmitterCtx Context)
  14. {
  15. EmitExceptionCall(Context, nameof(AThreadState.OnSvcCall));
  16. }
  17. private static void EmitExceptionCall(AILEmitterCtx Context, string MthdName)
  18. {
  19. AOpCodeException Op = (AOpCodeException)Context.CurrOp;
  20. Context.EmitStoreState();
  21. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  22. Context.EmitLdc_I8(Op.Position);
  23. Context.EmitLdc_I4(Op.Id);
  24. Context.EmitPrivateCall(typeof(AThreadState), 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(ATranslatedSub.StateArgIdx);
  28. Context.EmitCallPropGet(typeof(AThreadState), nameof(AThreadState.Running));
  29. AILLabel LblEnd = new AILLabel();
  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(Context.CurrBlock.Next);
  37. }
  38. else
  39. {
  40. Context.EmitLdc_I8(Op.Position + 4);
  41. Context.Emit(OpCodes.Ret);
  42. }
  43. }
  44. public static void Und(AILEmitterCtx Context)
  45. {
  46. AOpCode Op = Context.CurrOp;
  47. Context.EmitStoreState();
  48. Context.EmitLdarg(ATranslatedSub.StateArgIdx);
  49. Context.EmitLdc_I8(Op.Position);
  50. Context.EmitLdc_I4(Op.RawOpCode);
  51. Context.EmitPrivateCall(typeof(AThreadState), nameof(AThreadState.OnUndefined));
  52. if (Context.CurrBlock.Next != null)
  53. {
  54. Context.EmitLoadState(Context.CurrBlock.Next);
  55. }
  56. else
  57. {
  58. Context.EmitLdc_I8(Op.Position + 4);
  59. Context.Emit(OpCodes.Ret);
  60. }
  61. }
  62. }
  63. }