blob: 8755b1a3fb8e41f160c34859a64fbc092ef22082 [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 {
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +00007
8/// NUM_KEYS - Calculate the size of a const char* array.
9#define NUM_KEYS(Keys) sizeof(Keys) / sizeof(const char*)
10
11// See http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
12inline unsigned NextHighestPowerOf2 (unsigned i) {
13 i |= i >> 1;
14 i |= i >> 2;
15 i |= i >> 4;
16 i |= i >> 8;
17 i |= i >> 16;
18 i++;
19 return i;
20}
21
Mikhail Glushenkov84612022009-12-07 17:03:21 +000022typedef std::vector<std::string> StrVec;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000023typedef llvm::StringMap<const char*> ArgMap;
Mikhail Glushenkov84612022009-12-07 17:03:21 +000024
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000025/// ConvertToMAttrImpl - Common implementation of ConvertMArchToMAttr and
26/// ConvertToMAttr. The optional Args parameter contains information about how
27/// to transform special-cased values (for example, '-march=armv6' must be
28/// forwarded as '-mattr=+v6').
29std::string ConvertToMAttrImpl(const StrVec& Opts, const ArgMap* Args = 0) {
Mikhail Glushenkov84612022009-12-07 17:03:21 +000030 std::string out("-mattr=");
Mikhail Glushenkov84612022009-12-07 17:03:21 +000031 bool firstIter = true;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000032
Mikhail Glushenkov84612022009-12-07 17:03:21 +000033 for (StrVec::const_iterator B = Opts.begin(), E = Opts.end(); B!=E; ++B) {
34 const std::string& Arg = *B;
35
36 if (firstIter)
37 firstIter = false;
38 else
39 out += ",";
40
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000041 // Check if the argument is a special case.
42 if (Args != 0) {
43 ArgMap::const_iterator I = Args->find(Arg);
44
45 if (I != Args->end()) {
46 out += '+';
47 out += I->getValue();
48 continue;
49 }
50 }
51
52 // Convert 'no-foo' to '-foo'.
Mikhail Glushenkov84612022009-12-07 17:03:21 +000053 if (Arg.find("no-") == 0 && Arg[3] != 0) {
54 out += '-';
55 out += Arg.c_str() + 3;
56 }
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000057 // Convert 'foo' to '+foo'.
Mikhail Glushenkov84612022009-12-07 17:03:21 +000058 else {
59 out += '+';
60 out += Arg;
61 }
62 }
63
64 return out;
65}
66
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000067// -march values that need to be special-cased.
68const char* MArchKeysARM[] = { "armv4t", "armv5t", "armv5te", "armv6",
Mikhail Glushenkov26e10ae2010-12-15 01:22:15 +000069 "armv6-m", "armv6t2", "armv7-a", "armv7-m" };
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000070const char* MArchValuesARM[] = { "v4t", "v5t", "v5te", "v6", "v6m", "v6t2",
Mikhail Glushenkov26e10ae2010-12-15 01:22:15 +000071 "v7a", "v7m" };
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000072const unsigned MArchNumKeysARM = NUM_KEYS(MArchKeysARM);
73const unsigned MArchMapSize = NextHighestPowerOf2(MArchNumKeysARM);
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000074
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000075void FillInArgMap(ArgMap& Args, const char* Keys[],
76 const char* Values[], unsigned NumKeys)
77{
78 for (unsigned i = 0; i < NumKeys; ++i) {
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000079 // Explicit cast to StringRef here is necessary to pick up the right
80 // overload.
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000081 Args.GetOrCreateValue(llvm::StringRef(Keys[i]), Values[i]);
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000082 }
83}
84
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000085/// ConvertMArchToMAttr - Convert -march from the gcc dialect to
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000086/// something llc can understand.
87std::string ConvertMArchToMAttr(const StrVec& Opts) {
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000088 static ArgMap MArchMap(MArchMapSize);
89 static bool StaticDataInitialized = false;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000090
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000091 if (!StaticDataInitialized) {
92 FillInArgMap(MArchMap, MArchKeysARM, MArchValuesARM, MArchNumKeysARM);
93 StaticDataInitialized = true;
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +000094 }
95
96 return ConvertToMAttrImpl(Opts, &MArchMap);
97}
98
Mikhail Glushenkov2ac7eb82010-12-15 01:22:20 +000099// -mcpu values that need to be special-cased.
100const char* MCpuKeysPPC[] = { "G3", "G4", "G5", "powerpc", "powerpc64"};
101const char* MCpuValuesPPC[] = { "g3", "g4", "g5", "ppc", "ppc64"};
102const unsigned MCpuNumKeysPPC = NUM_KEYS(MCpuKeysPPC);
103const unsigned MCpuMapSize = NextHighestPowerOf2(MCpuNumKeysPPC);
104
105/// ConvertMCpu - Convert -mcpu value from the gcc to the llc dialect.
106std::string ConvertMCpu(const char* Val) {
107 static ArgMap MCpuMap(MCpuMapSize);
108 static bool StaticDataInitialized = false;
109
110 if (!StaticDataInitialized) {
111 FillInArgMap(MCpuMap, MCpuKeysPPC, MCpuValuesPPC, MCpuNumKeysPPC);
112 StaticDataInitialized = true;
113 }
114
115 std::string ret = "-mcpu=";
116 ArgMap::const_iterator I = MCpuMap.find(Val);
117 if (I != MCpuMap.end()) {
118 return ret + I->getValue();
119 }
120 return ret + Val;
121}
122
123// -mfpu values that need to be special-cased.
124const char* MFpuKeysARM[] = { "vfp", "vfpv3",
125 "vfpv3-fp16", "vfpv3-d16", "vfpv3-d16-fp16",
126 "neon", "neon-fp16" };
127const char* MFpuValuesARM[] = { "vfp2", "vfp3",
128 "+vfp3,+fp16", "+vfp3,+d16", "+vfp3,+d16,+fp16",
129 "+neon", "+neon,+neonfp" };
130const unsigned MFpuNumKeysARM = NUM_KEYS(MFpuKeysARM);
131const unsigned MFpuMapSize = NextHighestPowerOf2(MFpuNumKeysARM);
132
133/// ConvertMFpu - Convert -mfpu value from the gcc to the llc dialect.
134std::string ConvertMFpu(const char* Val) {
135 static ArgMap MFpuMap(MFpuMapSize);
136 static bool StaticDataInitialized = false;
137
138 if (!StaticDataInitialized) {
139 FillInArgMap(MFpuMap, MFpuKeysARM, MFpuValuesARM, MFpuNumKeysARM);
140 StaticDataInitialized = true;
141 }
142
143 std::string ret = "-mattr=";
144 ArgMap::const_iterator I = MFpuMap.find(Val);
145 if (I != MFpuMap.end()) {
146 return ret + I->getValue();
147 }
148 return ret + '+' + Val;
149}
150
Mikhail Glushenkov325f69d2010-11-28 00:31:13 +0000151/// ConvertToMAttr - Convert '-mfoo' and '-mno-bar' to '-mattr=+foo,-bar'.
152std::string ConvertToMAttr(const StrVec& Opts) {
153 return ConvertToMAttrImpl(Opts);
154}
155
Mikhail Glushenkov84612022009-12-07 17:03:21 +0000156}