| 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 | |
| 9 | #include "M68k.h" |
| 10 | #include "clang/Driver/Driver.h" |
| 11 | #include "clang/Driver/Options.h" |
| 12 | #include "llvm/ADT/StringSwitch.h" |
| 13 | #include "llvm/Option/ArgList.h" |
| 14 | #include "llvm/Support/Regex.h" |
| 15 | #include "llvm/TargetParser/Host.h" |
| 16 | |
| 17 | using namespace clang::driver; |
| 18 | using namespace clang::driver::tools; |
| 19 | using namespace clang; |
| 20 | using namespace llvm::opt; |
| 21 | |
| 22 | /// getM68kTargetCPU - Get the (LLVM) name of the 68000 cpu we are targeting. |
| 23 | std::string m68k::getM68kTargetCPU(const ArgList &Args) { |
| 24 | if (Arg *A = Args.getLastArg(Ids: clang::driver::options::OPT_mcpu_EQ)) { |
| 25 | // The canonical CPU name is captalize. However, we allow |
| 26 | // starting with lower case or numbers only |
| 27 | StringRef CPUName = A->getValue(); |
| 28 | |
| 29 | if (CPUName == "native" ) { |
| 30 | std::string CPU = std::string(llvm::sys::getHostCPUName()); |
| 31 | if (!CPU.empty() && CPU != "generic" ) |
| 32 | return CPU; |
| 33 | } |
| 34 | |
| 35 | if (CPUName == "common" ) |
| 36 | return "generic" ; |
| 37 | |
| 38 | return llvm::StringSwitch<std::string>(CPUName) |
| 39 | .Cases(S0: "m68000" , S1: "68000" , Value: "M68000" ) |
| 40 | .Cases(S0: "m68010" , S1: "68010" , Value: "M68010" ) |
| 41 | .Cases(S0: "m68020" , S1: "68020" , Value: "M68020" ) |
| 42 | .Cases(S0: "m68030" , S1: "68030" , Value: "M68030" ) |
| 43 | .Cases(S0: "m68040" , S1: "68040" , Value: "M68040" ) |
| 44 | .Cases(S0: "m68060" , S1: "68060" , Value: "M68060" ) |
| 45 | .Default(Value: CPUName.str()); |
| 46 | } |
| 47 | // FIXME: Throw error when multiple sub-architecture flag exist |
| 48 | if (Args.hasArg(Ids: clang::driver::options::OPT_m68000)) |
| 49 | return "M68000" ; |
| 50 | if (Args.hasArg(Ids: clang::driver::options::OPT_m68010)) |
| 51 | return "M68010" ; |
| 52 | if (Args.hasArg(Ids: clang::driver::options::OPT_m68020)) |
| 53 | return "M68020" ; |
| 54 | if (Args.hasArg(Ids: clang::driver::options::OPT_m68030)) |
| 55 | return "M68030" ; |
| 56 | if (Args.hasArg(Ids: clang::driver::options::OPT_m68040)) |
| 57 | return "M68040" ; |
| 58 | if (Args.hasArg(Ids: clang::driver::options::OPT_m68060)) |
| 59 | return "M68060" ; |
| 60 | |
| 61 | return "" ; |
| 62 | } |
| 63 | |
| 64 | static void addFloatABIFeatures(const llvm::opt::ArgList &Args, |
| 65 | std::vector<llvm::StringRef> &Features) { |
| 66 | Arg *A = Args.getLastArg(Ids: options::OPT_msoft_float, Ids: options::OPT_mhard_float, |
| 67 | Ids: options::OPT_m68881); |
| 68 | // Opt out FPU even for newer CPUs. |
| 69 | if (A && A->getOption().matches(ID: options::OPT_msoft_float)) { |
| 70 | Features.push_back(x: "-isa-68881" ); |
| 71 | Features.push_back(x: "-isa-68882" ); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | std::string CPU = m68k::getM68kTargetCPU(Args); |
| 76 | // Only enable M68881 for CPU < 68020 if the related flags are present. |
| 77 | if ((A && (CPU == "M68000" || CPU == "M68010" )) || |
| 78 | // Otherwise, by default we assume newer CPUs have M68881/2. |
| 79 | CPU == "M68020" ) |
| 80 | Features.push_back(x: "+isa-68881" ); |
| 81 | else if (CPU == "M68030" || CPU == "M68040" || CPU == "M68060" ) |
| 82 | // Note that although CPU >= M68040 imply M68882, we still add `isa-68882` |
| 83 | // anyway so that it's easier to add or not add the corresponding macro |
| 84 | // definitions later, in case we want to disable 68881/2 in newer CPUs |
| 85 | // (with -msoft-float, for instance). |
| 86 | Features.push_back(x: "+isa-68882" ); |
| 87 | } |
| 88 | |
| 89 | void m68k::getM68kTargetFeatures(const Driver &D, const llvm::Triple &Triple, |
| 90 | const ArgList &Args, |
| 91 | std::vector<StringRef> &Features) { |
| 92 | addFloatABIFeatures(Args, Features); |
| 93 | |
| 94 | // Handle '-ffixed-<register>' flags |
| 95 | if (Args.hasArg(Ids: options::OPT_ffixed_a0)) |
| 96 | Features.push_back(x: "+reserve-a0" ); |
| 97 | if (Args.hasArg(Ids: options::OPT_ffixed_a1)) |
| 98 | Features.push_back(x: "+reserve-a1" ); |
| 99 | if (Args.hasArg(Ids: options::OPT_ffixed_a2)) |
| 100 | Features.push_back(x: "+reserve-a2" ); |
| 101 | if (Args.hasArg(Ids: options::OPT_ffixed_a3)) |
| 102 | Features.push_back(x: "+reserve-a3" ); |
| 103 | if (Args.hasArg(Ids: options::OPT_ffixed_a4)) |
| 104 | Features.push_back(x: "+reserve-a4" ); |
| 105 | if (Args.hasArg(Ids: options::OPT_ffixed_a5)) |
| 106 | Features.push_back(x: "+reserve-a5" ); |
| 107 | if (Args.hasArg(Ids: options::OPT_ffixed_a6)) |
| 108 | Features.push_back(x: "+reserve-a6" ); |
| 109 | if (Args.hasArg(Ids: options::OPT_ffixed_d0)) |
| 110 | Features.push_back(x: "+reserve-d0" ); |
| 111 | if (Args.hasArg(Ids: options::OPT_ffixed_d1)) |
| 112 | Features.push_back(x: "+reserve-d1" ); |
| 113 | if (Args.hasArg(Ids: options::OPT_ffixed_d2)) |
| 114 | Features.push_back(x: "+reserve-d2" ); |
| 115 | if (Args.hasArg(Ids: options::OPT_ffixed_d3)) |
| 116 | Features.push_back(x: "+reserve-d3" ); |
| 117 | if (Args.hasArg(Ids: options::OPT_ffixed_d4)) |
| 118 | Features.push_back(x: "+reserve-d4" ); |
| 119 | if (Args.hasArg(Ids: options::OPT_ffixed_d5)) |
| 120 | Features.push_back(x: "+reserve-d5" ); |
| 121 | if (Args.hasArg(Ids: options::OPT_ffixed_d6)) |
| 122 | Features.push_back(x: "+reserve-d6" ); |
| 123 | if (Args.hasArg(Ids: options::OPT_ffixed_d7)) |
| 124 | Features.push_back(x: "+reserve-d7" ); |
| 125 | } |
| 126 | |