blob: e8fc9232e129a471de4f7b8fcf8134d078f05617 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- TargetInfo.cpp - Information about Target machine ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TargetInfo and TargetInfoImpl interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/TargetInfo.h"
15#include "clang/Basic/Diagnostic.h"
16#include "clang/AST/Builtins.h"
Chris Lattner525a0502007-09-22 18:29:59 +000017#include "llvm/ADT/APFloat.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000018#include "llvm/ADT/STLExtras.h"
Chris Lattner5483bdf2008-04-06 04:02:29 +000019#include <cstdlib>
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Chris Lattnercd4fc422008-03-08 08:59:43 +000022// TargetInfo Constructor.
23TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
24 // Set defaults. These should be overridden by concrete targets as needed.
25 CharIsSigned = true;
Chris Lattner927686f2008-05-09 06:08:39 +000026 PointerWidth = PointerAlign = 32;
Chris Lattnercd4fc422008-03-08 08:59:43 +000027 WCharWidth = WCharAlign = 32;
Chris Lattner2621fd12008-05-08 05:58:21 +000028 IntWidth = IntAlign = 32;
Chris Lattnerec10f582008-05-09 05:50:02 +000029 LongWidth = LongAlign = 32;
30 LongLongWidth = LongLongAlign = 64;
Nate Begeman3d292062008-04-18 17:17:24 +000031 DoubleWidth = 64;
32 DoubleAlign = 32;
Chris Lattnercd4fc422008-03-08 08:59:43 +000033 FloatFormat = &llvm::APFloat::IEEEsingle;
34 DoubleFormat = &llvm::APFloat::IEEEdouble;
35 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
36}
37
Chris Lattner0eaed122008-03-08 08:24:01 +000038// Out of line virtual dtor for TargetInfo.
39TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000040
Chris Lattner525a0502007-09-22 18:29:59 +000041//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +000042
Reid Spencer5f016e22007-07-11 17:01:13 +000043
Chris Lattner42e67372008-03-05 01:18:20 +000044static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlssonea041752008-02-06 00:11:32 +000045 if (Name[0] == '%' || Name[0] == '#')
46 Name++;
47}
48
Anders Carlsson3346ae62007-11-24 23:38:12 +000049/// isValidGCCRegisterName - Returns whether the passed in string
50/// is a valid register name according to GCC. This is used by Sema for
51/// inline asm statements.
52bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson6fa90862007-11-25 00:25:21 +000053 const char * const *Names;
54 unsigned NumNames;
55
56 // Get rid of any register prefix.
Anders Carlssonea041752008-02-06 00:11:32 +000057 removeGCCRegisterPrefix(Name);
58
Anders Carlsson6fa90862007-11-25 00:25:21 +000059
60 if (strcmp(Name, "memory") == 0 ||
61 strcmp(Name, "cc") == 0)
62 return true;
63
Chris Lattner0eaed122008-03-08 08:24:01 +000064 getGCCRegNames(Names, NumNames);
Anders Carlsson6fa90862007-11-25 00:25:21 +000065
66 // If we have a number it maps to an entry in the register name array.
67 if (isdigit(Name[0])) {
68 char *End;
69 int n = (int)strtol(Name, &End, 0);
70 if (*End == 0)
71 return n >= 0 && (unsigned)n < NumNames;
72 }
73
74 // Check register names.
75 for (unsigned i = 0; i < NumNames; i++) {
76 if (strcmp(Name, Names[i]) == 0)
77 return true;
78 }
79
80 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +000081 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +000082 unsigned NumAliases;
83
Chris Lattner0eaed122008-03-08 08:24:01 +000084 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +000085 for (unsigned i = 0; i < NumAliases; i++) {
86 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
87 if (!Aliases[i].Aliases[j])
88 break;
89 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
90 return true;
91 }
92 }
93
Anders Carlsson3346ae62007-11-24 23:38:12 +000094 return false;
95}
Anders Carlssond04c6e22007-11-27 04:11:28 +000096
Chris Lattner0eaed122008-03-08 08:24:01 +000097const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +000098 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
99
Anders Carlssonea041752008-02-06 00:11:32 +0000100 removeGCCRegisterPrefix(Name);
Anders Carlssonef3577d2008-02-05 23:30:20 +0000101
Anders Carlssond04c6e22007-11-27 04:11:28 +0000102 const char * const *Names;
103 unsigned NumNames;
104
Chris Lattner0eaed122008-03-08 08:24:01 +0000105 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000106
107 // First, check if we have a number.
108 if (isdigit(Name[0])) {
109 char *End;
110 int n = (int)strtol(Name, &End, 0);
111 if (*End == 0) {
112 assert(n >= 0 && (unsigned)n < NumNames &&
113 "Out of bounds register number!");
114 return Names[n];
115 }
116 }
117
118 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000119 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000120 unsigned NumAliases;
121
Chris Lattner0eaed122008-03-08 08:24:01 +0000122 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000123 for (unsigned i = 0; i < NumAliases; i++) {
124 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
125 if (!Aliases[i].Aliases[j])
126 break;
127 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
128 return Aliases[i].Register;
129 }
130 }
131
132 return Name;
133}
134
135bool TargetInfo::validateOutputConstraint(const char *Name,
136 ConstraintInfo &info) const
137{
138 // An output constraint must start with '=' or '+'
139 if (*Name != '=' && *Name != '+')
140 return false;
141
142 if (*Name == '+')
143 info = CI_ReadWrite;
144 else
145 info = CI_None;
146
147 Name++;
148 while (*Name) {
149 switch (*Name) {
150 default:
Chris Lattner0eaed122008-03-08 08:24:01 +0000151 if (!validateAsmConstraint(*Name, info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000152 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000153 // so we can add more constraints as we hit it.
154 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000155 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000156 }
157 case '&': // early clobber.
158 break;
159 case 'r': // general register.
160 info = (ConstraintInfo)(info|CI_AllowsRegister);
161 break;
162 case 'm': // memory operand.
163 info = (ConstraintInfo)(info|CI_AllowsMemory);
164 break;
165 case 'g': // general register, memory operand or immediate integer.
166 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
167 break;
168 }
169
170 Name++;
171 }
172
173 return true;
174}
175
176bool TargetInfo::validateInputConstraint(const char *Name,
177 unsigned NumOutputs,
Chris Lattner0eaed122008-03-08 08:24:01 +0000178 ConstraintInfo &info) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000179 while (*Name) {
180 switch (*Name) {
181 default:
182 // Check if we have a matching constraint
183 if (*Name >= '0' && *Name <= '9') {
184 unsigned i = *Name - '0';
185
186 // Check if matching constraint is out of bounds.
187 if (i >= NumOutputs)
188 return false;
Chris Lattner0eaed122008-03-08 08:24:01 +0000189 } else if (!validateAsmConstraint(*Name, info)) {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000190 // FIXME: This assert is in place temporarily
191 // so we can add more constraints as we hit it.
192 // Eventually, an unknown constraint should just be treated as 'g'.
193 assert(0 && "Unknown input constraint type!");
194 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000195 case '%': // commutative
196 // FIXME: Fail if % is used with the last operand.
197 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000198 case 'i': // immediate integer.
Anders Carlssonb41edf92008-02-18 17:00:25 +0000199 case 'I':
Anders Carlssonefdfa772008-03-09 06:02:02 +0000200 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000201 break;
202 case 'r': // general register.
203 info = (ConstraintInfo)(info|CI_AllowsRegister);
204 break;
205 case 'm': // memory operand.
206 info = (ConstraintInfo)(info|CI_AllowsMemory);
207 break;
208 case 'g': // general register, memory operand or immediate integer.
209 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
210 break;
211 }
212
213 Name++;
214 }
215
216 return true;
217}