blob: 4ce32a9972fb04e017d39d171964f2bc88e3106d [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;
Nate Begemand5e35f82008-04-18 17:17:24 +000027 DoubleWidth = 64;
28 DoubleAlign = 32;
Chris Lattner3b74cac2008-03-08 08:59:43 +000029 FloatFormat = &llvm::APFloat::IEEEsingle;
30 DoubleFormat = &llvm::APFloat::IEEEdouble;
31 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
32}
33
Chris Lattner0fb8d852008-03-08 08:24:01 +000034// Out of line virtual dtor for TargetInfo.
35TargetInfo::~TargetInfo() {}
Chris Lattner4b009652007-07-25 00:24:17 +000036
Chris Lattner858eece2007-09-22 18:29:59 +000037//===----------------------------------------------------------------------===//
Chris Lattner858eece2007-09-22 18:29:59 +000038
Chris Lattner4b009652007-07-25 00:24:17 +000039
Chris Lattnerfc457002008-03-05 01:18:20 +000040static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlsson74385982008-02-06 00:11:32 +000041 if (Name[0] == '%' || Name[0] == '#')
42 Name++;
43}
44
Anders Carlsson7dd1c952007-11-24 23:38:12 +000045/// isValidGCCRegisterName - Returns whether the passed in string
46/// is a valid register name according to GCC. This is used by Sema for
47/// inline asm statements.
48bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson49dadd62007-11-25 00:25:21 +000049 const char * const *Names;
50 unsigned NumNames;
51
52 // Get rid of any register prefix.
Anders Carlsson74385982008-02-06 00:11:32 +000053 removeGCCRegisterPrefix(Name);
54
Anders Carlsson49dadd62007-11-25 00:25:21 +000055
56 if (strcmp(Name, "memory") == 0 ||
57 strcmp(Name, "cc") == 0)
58 return true;
59
Chris Lattner0fb8d852008-03-08 08:24:01 +000060 getGCCRegNames(Names, NumNames);
Anders Carlsson49dadd62007-11-25 00:25:21 +000061
62 // If we have a number it maps to an entry in the register name array.
63 if (isdigit(Name[0])) {
64 char *End;
65 int n = (int)strtol(Name, &End, 0);
66 if (*End == 0)
67 return n >= 0 && (unsigned)n < NumNames;
68 }
69
70 // Check register names.
71 for (unsigned i = 0; i < NumNames; i++) {
72 if (strcmp(Name, Names[i]) == 0)
73 return true;
74 }
75
76 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +000077 const GCCRegAlias *Aliases;
Anders Carlsson49dadd62007-11-25 00:25:21 +000078 unsigned NumAliases;
79
Chris Lattner0fb8d852008-03-08 08:24:01 +000080 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson49dadd62007-11-25 00:25:21 +000081 for (unsigned i = 0; i < NumAliases; i++) {
82 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
83 if (!Aliases[i].Aliases[j])
84 break;
85 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
86 return true;
87 }
88 }
89
Anders Carlsson7dd1c952007-11-24 23:38:12 +000090 return false;
91}
Anders Carlsson4ce42302007-11-27 04:11:28 +000092
Chris Lattner0fb8d852008-03-08 08:24:01 +000093const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +000094 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
95
Anders Carlsson74385982008-02-06 00:11:32 +000096 removeGCCRegisterPrefix(Name);
Anders Carlsson3fb39b12008-02-05 23:30:20 +000097
Anders Carlsson4ce42302007-11-27 04:11:28 +000098 const char * const *Names;
99 unsigned NumNames;
100
Chris Lattner0fb8d852008-03-08 08:24:01 +0000101 getGCCRegNames(Names, NumNames);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000102
103 // First, check if we have a number.
104 if (isdigit(Name[0])) {
105 char *End;
106 int n = (int)strtol(Name, &End, 0);
107 if (*End == 0) {
108 assert(n >= 0 && (unsigned)n < NumNames &&
109 "Out of bounds register number!");
110 return Names[n];
111 }
112 }
113
114 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000115 const GCCRegAlias *Aliases;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000116 unsigned NumAliases;
117
Chris Lattner0fb8d852008-03-08 08:24:01 +0000118 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000119 for (unsigned i = 0; i < NumAliases; i++) {
120 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
121 if (!Aliases[i].Aliases[j])
122 break;
123 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
124 return Aliases[i].Register;
125 }
126 }
127
128 return Name;
129}
130
131bool TargetInfo::validateOutputConstraint(const char *Name,
132 ConstraintInfo &info) const
133{
134 // An output constraint must start with '=' or '+'
135 if (*Name != '=' && *Name != '+')
136 return false;
137
138 if (*Name == '+')
139 info = CI_ReadWrite;
140 else
141 info = CI_None;
142
143 Name++;
144 while (*Name) {
145 switch (*Name) {
146 default:
Chris Lattner0fb8d852008-03-08 08:24:01 +0000147 if (!validateAsmConstraint(*Name, info)) {
Ted Kremenekd229d962008-04-24 16:36:38 +0000148 // FIXME: We temporarily return false
Anders Carlsson4ce42302007-11-27 04:11:28 +0000149 // so we can add more constraints as we hit it.
150 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekd229d962008-04-24 16:36:38 +0000151 return false;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000152 }
153 case '&': // early clobber.
154 break;
155 case 'r': // general register.
156 info = (ConstraintInfo)(info|CI_AllowsRegister);
157 break;
158 case 'm': // memory operand.
159 info = (ConstraintInfo)(info|CI_AllowsMemory);
160 break;
161 case 'g': // general register, memory operand or immediate integer.
162 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
163 break;
164 }
165
166 Name++;
167 }
168
169 return true;
170}
171
172bool TargetInfo::validateInputConstraint(const char *Name,
173 unsigned NumOutputs,
Chris Lattner0fb8d852008-03-08 08:24:01 +0000174 ConstraintInfo &info) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000175 while (*Name) {
176 switch (*Name) {
177 default:
178 // Check if we have a matching constraint
179 if (*Name >= '0' && *Name <= '9') {
180 unsigned i = *Name - '0';
181
182 // Check if matching constraint is out of bounds.
183 if (i >= NumOutputs)
184 return false;
Chris Lattner0fb8d852008-03-08 08:24:01 +0000185 } else if (!validateAsmConstraint(*Name, info)) {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000186 // FIXME: This assert is in place temporarily
187 // so we can add more constraints as we hit it.
188 // Eventually, an unknown constraint should just be treated as 'g'.
189 assert(0 && "Unknown input constraint type!");
190 }
Anders Carlsson333052c2007-12-08 19:32:57 +0000191 case '%': // commutative
192 // FIXME: Fail if % is used with the last operand.
193 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000194 case 'i': // immediate integer.
Anders Carlssonf8e50d92008-02-18 17:00:25 +0000195 case 'I':
Anders Carlssonbdb0e2b2008-03-09 06:02:02 +0000196 case 'n': // immediate integer with a known value.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000197 break;
198 case 'r': // general register.
199 info = (ConstraintInfo)(info|CI_AllowsRegister);
200 break;
201 case 'm': // memory operand.
202 info = (ConstraintInfo)(info|CI_AllowsMemory);
203 break;
204 case 'g': // general register, memory operand or immediate integer.
205 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
206 break;
207 }
208
209 Name++;
210 }
211
212 return true;
213}