M68k.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //===--- M68k.cpp - M68k Helpers for Tools -------------------*- C++-*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "M68k.h"
  9. #include "ToolChains/CommonArgs.h"
  10. #include "clang/Driver/Driver.h"
  11. #include "clang/Driver/DriverDiagnostic.h"
  12. #include "clang/Driver/Options.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/ADT/StringSwitch.h"
  15. #include "llvm/Option/ArgList.h"
  16. #include "llvm/Support/Host.h"
  17. #include "llvm/Support/Regex.h"
  18. #include <sstream>
  19. using namespace clang::driver;
  20. using namespace clang::driver::tools;
  21. using namespace clang;
  22. using namespace llvm::opt;
  23. /// getM68kTargetCPU - Get the (LLVM) name of the 68000 cpu we are targeting.
  24. std::string m68k::getM68kTargetCPU(const ArgList &Args) {
  25. if (Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ)) {
  26. // The canonical CPU name is captalize. However, we allow
  27. // starting with lower case or numbers only
  28. StringRef CPUName = A->getValue();
  29. if (CPUName == "native") {
  30. std::string CPU = std::string(llvm::sys::getHostCPUName());
  31. if (!CPU.empty() && CPU != "generic")
  32. return CPU;
  33. }
  34. if (CPUName == "common")
  35. return "generic";
  36. return llvm::StringSwitch<std::string>(CPUName)
  37. .Cases("m68000", "68000", "M68000")
  38. .Cases("m68010", "68010", "M68010")
  39. .Cases("m68020", "68020", "M68020")
  40. .Cases("m68030", "68030", "M68030")
  41. .Cases("m68040", "68040", "M68040")
  42. .Cases("m68060", "68060", "M68060")
  43. .Default(CPUName.str());
  44. }
  45. // FIXME: Throw error when multiple sub-architecture flag exist
  46. if (Args.hasArg(clang::driver::options::OPT_m68000))
  47. return "M68000";
  48. if (Args.hasArg(clang::driver::options::OPT_m68010))
  49. return "M68010";
  50. if (Args.hasArg(clang::driver::options::OPT_m68020))
  51. return "M68020";
  52. if (Args.hasArg(clang::driver::options::OPT_m68030))
  53. return "M68030";
  54. if (Args.hasArg(clang::driver::options::OPT_m68040))
  55. return "M68040";
  56. if (Args.hasArg(clang::driver::options::OPT_m68060))
  57. return "M68060";
  58. return "";
  59. }
  60. void m68k::getM68kTargetFeatures(const Driver &D, const llvm::Triple &Triple,
  61. const ArgList &Args,
  62. std::vector<StringRef> &Features) {
  63. m68k::FloatABI FloatABI = m68k::getM68kFloatABI(D, Args);
  64. if (FloatABI == m68k::FloatABI::Soft)
  65. Features.push_back("-hard-float");
  66. // Handle '-ffixed-<register>' flags
  67. if (Args.hasArg(options::OPT_ffixed_a0))
  68. Features.push_back("+reserve-a0");
  69. if (Args.hasArg(options::OPT_ffixed_a1))
  70. Features.push_back("+reserve-a1");
  71. if (Args.hasArg(options::OPT_ffixed_a2))
  72. Features.push_back("+reserve-a2");
  73. if (Args.hasArg(options::OPT_ffixed_a3))
  74. Features.push_back("+reserve-a3");
  75. if (Args.hasArg(options::OPT_ffixed_a4))
  76. Features.push_back("+reserve-a4");
  77. if (Args.hasArg(options::OPT_ffixed_a5))
  78. Features.push_back("+reserve-a5");
  79. if (Args.hasArg(options::OPT_ffixed_a6))
  80. Features.push_back("+reserve-a6");
  81. if (Args.hasArg(options::OPT_ffixed_d0))
  82. Features.push_back("+reserve-d0");
  83. if (Args.hasArg(options::OPT_ffixed_d1))
  84. Features.push_back("+reserve-d1");
  85. if (Args.hasArg(options::OPT_ffixed_d2))
  86. Features.push_back("+reserve-d2");
  87. if (Args.hasArg(options::OPT_ffixed_d3))
  88. Features.push_back("+reserve-d3");
  89. if (Args.hasArg(options::OPT_ffixed_d4))
  90. Features.push_back("+reserve-d4");
  91. if (Args.hasArg(options::OPT_ffixed_d5))
  92. Features.push_back("+reserve-d5");
  93. if (Args.hasArg(options::OPT_ffixed_d6))
  94. Features.push_back("+reserve-d6");
  95. if (Args.hasArg(options::OPT_ffixed_d7))
  96. Features.push_back("+reserve-d7");
  97. }
  98. m68k::FloatABI m68k::getM68kFloatABI(const Driver &D, const ArgList &Args) {
  99. m68k::FloatABI ABI = m68k::FloatABI::Invalid;
  100. if (Arg *A =
  101. Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float)) {
  102. if (A->getOption().matches(options::OPT_msoft_float))
  103. ABI = m68k::FloatABI::Soft;
  104. else if (A->getOption().matches(options::OPT_mhard_float))
  105. ABI = m68k::FloatABI::Hard;
  106. }
  107. // If unspecified, choose the default based on the platform.
  108. if (ABI == m68k::FloatABI::Invalid)
  109. ABI = m68k::FloatABI::Hard;
  110. return ABI;
  111. }