blob: 2dc6b8b916120c79f154f82d86c966ef99556408 [file] [log] [blame]
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +00001#include "llvm/ADT/StringMap.h"
2
Mikhail Glushenkov84612022009-12-07 17:03:21 +00003#include <string>
4#include <vector>
5
6namespace hooks {
7typedef std::vector<std::string> StrVec;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +00008typedef llvm::StringMap<const char*> ArgMap;
Mikhail Glushenkov84612022009-12-07 17:03:21 +00009
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000010/// ConvertToMAttrImpl - Common implementation of ConvertMArchToMAttr and
11/// ConvertToMAttr. The optional Args parameter contains information about how
12/// to transform special-cased values (for example, '-march=armv6' must be
13/// forwarded as '-mattr=+v6').
14std::string ConvertToMAttrImpl(const StrVec& Opts, const ArgMap* Args = 0) {
Mikhail Glushenkov84612022009-12-07 17:03:21 +000015 std::string out("-mattr=");
Mikhail Glushenkov84612022009-12-07 17:03:21 +000016 bool firstIter = true;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000017
Mikhail Glushenkov84612022009-12-07 17:03:21 +000018 for (StrVec::const_iterator B = Opts.begin(), E = Opts.end(); B!=E; ++B) {
19 const std::string& Arg = *B;
20
21 if (firstIter)
22 firstIter = false;
23 else
24 out += ",";
25
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000026 // Check if the argument is a special case.
27 if (Args != 0) {
28 ArgMap::const_iterator I = Args->find(Arg);
29
30 if (I != Args->end()) {
31 out += '+';
32 out += I->getValue();
33 continue;
34 }
35 }
36
37 // Convert 'no-foo' to '-foo'.
Mikhail Glushenkov84612022009-12-07 17:03:21 +000038 if (Arg.find("no-") == 0 && Arg[3] != 0) {
39 out += '-';
40 out += Arg.c_str() + 3;
41 }
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000042 // Convert 'foo' to '+foo'.
Mikhail Glushenkov84612022009-12-07 17:03:21 +000043 else {
44 out += '+';
45 out += Arg;
46 }
47 }
48
49 return out;
50}
51
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000052// Values needed to be special-cased by ConvertMArchToMAttr.
Mikhail Glushenkov26e10ae2010-12-15 01:22:15 +000053const char* MArchMapKeys[] = { "armv4t", "armv5t", "armv5te", "armv6",
54 "armv6-m", "armv6t2", "armv7-a", "armv7-m" };
55const char* MArchMapValues[] = { "v4t", "v5t", "v5te", "v6", "v6m", "v6t2",
56 "v7a", "v7m" };
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000057const unsigned NumMArchMapKeys = sizeof(MArchMapKeys) / sizeof(const char*);
58
59void InitializeMArchMap(ArgMap& Args) {
60 for (unsigned i = 0; i < NumMArchMapKeys; ++i) {
61 // Explicit cast to StringRef here is necessary to pick up the right
62 // overload.
63 Args.GetOrCreateValue(llvm::StringRef(MArchMapKeys[i]), MArchMapValues[i]);
64 }
65}
66
67/// ConvertMArchToMAttr - Try to convert -march from the gcc dialect to
68/// something llc can understand.
69std::string ConvertMArchToMAttr(const StrVec& Opts) {
70 static ArgMap MArchMap(NumMArchMapKeys);
71 static bool MArchMapInitialized = false;
72
73 if (!MArchMapInitialized) {
74 InitializeMArchMap(MArchMap);
75 MArchMapInitialized = true;
76 }
77
78 return ConvertToMAttrImpl(Opts, &MArchMap);
79}
80
81/// ConvertToMAttr - Convert '-mfoo' and '-mno-bar' to '-mattr=+foo,-bar'.
82std::string ConvertToMAttr(const StrVec& Opts) {
83 return ConvertToMAttrImpl(Opts);
84}
85
Mikhail Glushenkov84612022009-12-07 17:03:21 +000086}