blob: efc91cf407c58986eb19e3c5925d90bc90b8a050 [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.
53const char* MArchMapKeys[] = { "armv6" };
54const char* MArchMapValues[] = { "v6" };
55const unsigned NumMArchMapKeys = sizeof(MArchMapKeys) / sizeof(const char*);
56
57void InitializeMArchMap(ArgMap& Args) {
58 for (unsigned i = 0; i < NumMArchMapKeys; ++i) {
59 // Explicit cast to StringRef here is necessary to pick up the right
60 // overload.
61 Args.GetOrCreateValue(llvm::StringRef(MArchMapKeys[i]), MArchMapValues[i]);
62 }
63}
64
65/// ConvertMArchToMAttr - Try to convert -march from the gcc dialect to
66/// something llc can understand.
67std::string ConvertMArchToMAttr(const StrVec& Opts) {
68 static ArgMap MArchMap(NumMArchMapKeys);
69 static bool MArchMapInitialized = false;
70
71 if (!MArchMapInitialized) {
72 InitializeMArchMap(MArchMap);
73 MArchMapInitialized = true;
74 }
75
76 return ConvertToMAttrImpl(Opts, &MArchMap);
77}
78
79/// ConvertToMAttr - Convert '-mfoo' and '-mno-bar' to '-mattr=+foo,-bar'.
80std::string ConvertToMAttr(const StrVec& Opts) {
81 return ConvertToMAttrImpl(Opts);
82}
83
Mikhail Glushenkov84612022009-12-07 17:03:21 +000084}