Compiler.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using ARMeilleure.CodeGen;
  2. using ARMeilleure.CodeGen.Optimizations;
  3. using ARMeilleure.Diagnostics;
  4. using ARMeilleure.IntermediateRepresentation;
  5. using System;
  6. using System.Runtime.InteropServices;
  7. namespace ARMeilleure.Translation
  8. {
  9. static class Compiler
  10. {
  11. public static CompiledFunction Compile(
  12. ControlFlowGraph cfg,
  13. OperandType[] argTypes,
  14. OperandType retType,
  15. CompilerOptions options,
  16. Architecture target)
  17. {
  18. CompilerContext cctx = new(cfg, argTypes, retType, options);
  19. if (options.HasFlag(CompilerOptions.Optimize))
  20. {
  21. Logger.StartPass(PassName.TailMerge);
  22. TailMerge.RunPass(cctx);
  23. Logger.EndPass(PassName.TailMerge, cfg);
  24. }
  25. if (options.HasFlag(CompilerOptions.SsaForm))
  26. {
  27. Logger.StartPass(PassName.Dominance);
  28. Dominance.FindDominators(cfg);
  29. Dominance.FindDominanceFrontiers(cfg);
  30. Logger.EndPass(PassName.Dominance);
  31. Logger.StartPass(PassName.SsaConstruction);
  32. Ssa.Construct(cfg);
  33. Logger.EndPass(PassName.SsaConstruction, cfg);
  34. }
  35. else
  36. {
  37. Logger.StartPass(PassName.RegisterToLocal);
  38. RegisterToLocal.Rename(cfg);
  39. Logger.EndPass(PassName.RegisterToLocal, cfg);
  40. }
  41. if (target == Architecture.X64)
  42. {
  43. return CodeGen.X86.CodeGenerator.Generate(cctx);
  44. }
  45. else if (target == Architecture.Arm64)
  46. {
  47. return CodeGen.Arm64.CodeGenerator.Generate(cctx);
  48. }
  49. else
  50. {
  51. throw new NotImplementedException(target.ToString());
  52. }
  53. }
  54. }
  55. }