blob: 675406edef3f686313beac9cea9d562d7661bba1 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- TargetInfo.cpp - Information about Target machine ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner858eece2007-09-22 18:29:59 +000017#include "llvm/ADT/APFloat.h"
Anders Carlsson49dadd62007-11-25 00:25:21 +000018#include "llvm/ADT/STLExtras.h"
Chris Lattner5c81ea02008-04-06 04:02:29 +000019#include <cstdlib>
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Chris Lattner3b74cac2008-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;
26 WCharWidth = WCharAlign = 32;
Chris Lattner85970f32008-05-08 05:58:21 +000027 IntWidth = IntAlign = 32;
Nate Begemand5e35f82008-04-18 17:17:24 +000028 DoubleWidth = 64;
29 DoubleAlign = 32;
Chris Lattner3b74cac2008-03-08 08:59:43 +000030 FloatFormat = &llvm::APFloat::IEEEsingle;
31 DoubleFormat = &llvm::APFloat::IEEEdouble;
32 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
33}
34
Chris Lattner0fb8d852008-03-08 08:24:01 +000035// Out of line virtual dtor for TargetInfo.
36TargetInfo::~TargetInfo() {}
Chris Lattner4b009652007-07-25 00:24:17 +000037
Chris Lattner858eece2007-09-22 18:29:59 +000038//===----------------------------------------------------------------------===//
Chris Lattner858eece2007-09-22 18:29:59 +000039
Chris Lattner4b009652007-07-25 00:24:17 +000040
Chris Lattnerfc457002008-03-05 01:18:20 +000041static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlsson74385982008-02-06 00:11:32 +000042 if (Name[0] == '%' || Name[0] == '#')
43 Name++;
44}
45
Anders Carlsson7dd1c952007-11-24 23:38:12 +000046/// isValidGCCRegisterName - Returns whether the passed in string
47/// is a valid register name according to GCC. This is used by Sema for
48/// inline asm statements.
49bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson49dadd62007-11-25 00:25:21 +000050 const char * const *Names;
51 unsigned NumNames;
52
53 // Get rid of any register prefix.
Anders Carlsson74385982008-02-06 00:11:32 +000054 removeGCCRegisterPrefix(Name);
55
Anders Carlsson49dadd62007-11-25 00:25:21 +000056
57 if (strcmp(Name, "memory") == 0 ||
58 strcmp(Name, "cc") == 0)
59 return true;
60
Chris Lattner0fb8d852008-03-08 08:24:01 +000061 getGCCRegNames(Names, NumNames);
Anders Carlsson49dadd62007-11-25 00:25:21 +000062
63 // If we have a number it maps to an entry in the register name array.
64 if (isdigit(Name[0])) {
65 char *End;
66 int n = (int)strtol(Name, &End, 0);
67 if (*End == 0)
68 return n >= 0 && (unsigned)n < NumNames;
69 }
70
71 // Check register names.
72 for (unsigned i = 0; i < NumNames; i++) {
73 if (strcmp(Name, Names[i]) == 0)
74 return true;
75 }
76
77 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +000078 const GCCRegAlias *Aliases;
Anders Carlsson49dadd62007-11-25 00:25:21 +000079 unsigned NumAliases;
80
Chris Lattner0fb8d852008-03-08 08:24:01 +000081 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson49dadd62007-11-25 00:25:21 +000082 for (unsigned i = 0; i < NumAliases; i++) {
83 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
84 if (!Aliases[i].Aliases[j])
85 break;
86 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
87 return true;
88 }
89 }
90
Anders Carlsson7dd1c952007-11-24 23:38:12 +000091 return false;
92}
Anders Carlsson4ce42302007-11-27 04:11:28 +000093
Chris Lattner0fb8d852008-03-08 08:24:01 +000094const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +000095 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
96
Anders Carlsson74385982008-02-06 00:11:32 +000097 removeGCCRegisterPrefix(Name);
Anders Carlsson3fb39b12008-02-05 23:30:20 +000098
Anders Carlsson4ce42302007-11-27 04:11:28 +000099 const char * const *Names;
100 unsigned NumNames;
101
Chris Lattner0fb8d852008-03-08 08:24:01 +0000102 getGCCRegNames(Names, NumNames);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000103
104 // First, check if we have a number.
105 if (isdigit(Name[0])) {
106 char *End;
107 int n = (int)strtol(Name, &End, 0);
108 if (*End == 0) {
109 assert(n >= 0 && (unsigned)n < NumNames &&
110 "Out of bounds register number!");
111 return Names[n];
112 }
113 }
114
115 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000116 const GCCRegAlias *Aliases;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000117 unsigned NumAliases;
118
Chris Lattner0fb8d852008-03-08 08:24:01 +0000119 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000120 for (unsigned i = 0; i < NumAliases; i++) {
121 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
122 if (!Aliases[i].Aliases[j])
123 break;
124 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
125 return Aliases[i].Register;
126 }
127 }
128
129 return Name;
130}
131
132bool TargetInfo::validateOutputConstraint(const char *Name,
133 ConstraintInfo &info) const
134{
135 // An output constraint must start with '=' or '+'
136 if (*Name != '=' && *Name != '+')
137 return false;
138
139 if (*Name == '+')
140 info = CI_ReadWrite;
141 else
142 info = CI_None;
143
144 Name++;
145 while (*Name) {
146 switch (*Name) {
147 default:
Chris Lattner0fb8d852008-03-08 08:24:01 +0000148 if (!validateAsmConstraint(*Name, info)) {
Ted Kremenekd229d962008-04-24 16:36:38 +0000149 // FIXME: We temporarily return false
Anders Carlsson4ce42302007-11-27 04:11:28 +0000150 // so we can add more constraints as we hit it.
151 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekd229d962008-04-24 16:36:38 +0000152 return false;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000153 }
154 case '&': // early clobber.
155 break;
156 case 'r': // general register.
157 info = (ConstraintInfo)(info|CI_AllowsRegister);
158 break;
159 case 'm': // memory operand.
160 info = (ConstraintInfo)(info|CI_AllowsMemory);
161 break;
162 case 'g': // general register, memory operand or immediate integer.
163 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
164 break;
165 }
166
167 Name++;
168 }
169
170 return true;
171}
172
173bool TargetInfo::validateInputConstraint(const char *Name,
174 unsigned NumOutputs,
Chris Lattner0fb8d852008-03-08 08:24:01 +0000175 ConstraintInfo &info) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000176 while (*Name) {
177 switch (*Name) {
178 default:
179 // Check if we have a matching constraint
180 if (*Name >= '0' && *Name <= '9') {
181 unsigned i = *Name - '0';
182
183 // Check if matching constraint is out of bounds.
184 if (i >= NumOutputs)
185 return false;
Chris Lattner0fb8d852008-03-08 08:24:01 +0000186 } else if (!validateAsmConstraint(*Name, info)) {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000187 // FIXME: This assert is in place temporarily
188 // so we can add more constraints as we hit it.
189 // Eventually, an unknown constraint should just be treated as 'g'.
190 assert(0 && "Unknown input constraint type!");
191 }
Anders Carlsson333052c2007-12-08 19:32:57 +0000192 case '%': // commutative
193 // FIXME: Fail if % is used with the last operand.
194 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000195 case 'i': // immediate integer.
Anders Carlssonf8e50d92008-02-18 17:00:25 +0000196 case 'I':
Anders Carlssonbdb0e2b2008-03-09 06:02:02 +0000197 case 'n': // immediate integer with a known value.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000198 break;
199 case 'r': // general register.
200 info = (ConstraintInfo)(info|CI_AllowsRegister);
201 break;
202 case 'm': // memory operand.
203 info = (ConstraintInfo)(info|CI_AllowsMemory);
204 break;
205 case 'g': // general register, memory operand or immediate integer.
206 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
207 break;
208 }
209
210 Name++;
211 }
212
213 return true;
214}