blob: dd1fe648d42ee88599f86e98b1329c6e6669ffcb [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;
Chris Lattner481dcf22008-05-09 05:50:02 +000028 LongWidth = LongAlign = 32;
29 LongLongWidth = LongLongAlign = 64;
Nate Begemand5e35f82008-04-18 17:17:24 +000030 DoubleWidth = 64;
31 DoubleAlign = 32;
Chris Lattner3b74cac2008-03-08 08:59:43 +000032 FloatFormat = &llvm::APFloat::IEEEsingle;
33 DoubleFormat = &llvm::APFloat::IEEEdouble;
34 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
35}
36
Chris Lattner0fb8d852008-03-08 08:24:01 +000037// Out of line virtual dtor for TargetInfo.
38TargetInfo::~TargetInfo() {}
Chris Lattner4b009652007-07-25 00:24:17 +000039
Chris Lattner858eece2007-09-22 18:29:59 +000040//===----------------------------------------------------------------------===//
Chris Lattner858eece2007-09-22 18:29:59 +000041
Chris Lattner4b009652007-07-25 00:24:17 +000042
Chris Lattnerfc457002008-03-05 01:18:20 +000043static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlsson74385982008-02-06 00:11:32 +000044 if (Name[0] == '%' || Name[0] == '#')
45 Name++;
46}
47
Anders Carlsson7dd1c952007-11-24 23:38:12 +000048/// isValidGCCRegisterName - Returns whether the passed in string
49/// is a valid register name according to GCC. This is used by Sema for
50/// inline asm statements.
51bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson49dadd62007-11-25 00:25:21 +000052 const char * const *Names;
53 unsigned NumNames;
54
55 // Get rid of any register prefix.
Anders Carlsson74385982008-02-06 00:11:32 +000056 removeGCCRegisterPrefix(Name);
57
Anders Carlsson49dadd62007-11-25 00:25:21 +000058
59 if (strcmp(Name, "memory") == 0 ||
60 strcmp(Name, "cc") == 0)
61 return true;
62
Chris Lattner0fb8d852008-03-08 08:24:01 +000063 getGCCRegNames(Names, NumNames);
Anders Carlsson49dadd62007-11-25 00:25:21 +000064
65 // If we have a number it maps to an entry in the register name array.
66 if (isdigit(Name[0])) {
67 char *End;
68 int n = (int)strtol(Name, &End, 0);
69 if (*End == 0)
70 return n >= 0 && (unsigned)n < NumNames;
71 }
72
73 // Check register names.
74 for (unsigned i = 0; i < NumNames; i++) {
75 if (strcmp(Name, Names[i]) == 0)
76 return true;
77 }
78
79 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +000080 const GCCRegAlias *Aliases;
Anders Carlsson49dadd62007-11-25 00:25:21 +000081 unsigned NumAliases;
82
Chris Lattner0fb8d852008-03-08 08:24:01 +000083 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson49dadd62007-11-25 00:25:21 +000084 for (unsigned i = 0; i < NumAliases; i++) {
85 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
86 if (!Aliases[i].Aliases[j])
87 break;
88 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
89 return true;
90 }
91 }
92
Anders Carlsson7dd1c952007-11-24 23:38:12 +000093 return false;
94}
Anders Carlsson4ce42302007-11-27 04:11:28 +000095
Chris Lattner0fb8d852008-03-08 08:24:01 +000096const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +000097 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
98
Anders Carlsson74385982008-02-06 00:11:32 +000099 removeGCCRegisterPrefix(Name);
Anders Carlsson3fb39b12008-02-05 23:30:20 +0000100
Anders Carlsson4ce42302007-11-27 04:11:28 +0000101 const char * const *Names;
102 unsigned NumNames;
103
Chris Lattner0fb8d852008-03-08 08:24:01 +0000104 getGCCRegNames(Names, NumNames);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000105
106 // First, check if we have a number.
107 if (isdigit(Name[0])) {
108 char *End;
109 int n = (int)strtol(Name, &End, 0);
110 if (*End == 0) {
111 assert(n >= 0 && (unsigned)n < NumNames &&
112 "Out of bounds register number!");
113 return Names[n];
114 }
115 }
116
117 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000118 const GCCRegAlias *Aliases;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000119 unsigned NumAliases;
120
Chris Lattner0fb8d852008-03-08 08:24:01 +0000121 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000122 for (unsigned i = 0; i < NumAliases; i++) {
123 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
124 if (!Aliases[i].Aliases[j])
125 break;
126 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
127 return Aliases[i].Register;
128 }
129 }
130
131 return Name;
132}
133
134bool TargetInfo::validateOutputConstraint(const char *Name,
135 ConstraintInfo &info) const
136{
137 // An output constraint must start with '=' or '+'
138 if (*Name != '=' && *Name != '+')
139 return false;
140
141 if (*Name == '+')
142 info = CI_ReadWrite;
143 else
144 info = CI_None;
145
146 Name++;
147 while (*Name) {
148 switch (*Name) {
149 default:
Chris Lattner0fb8d852008-03-08 08:24:01 +0000150 if (!validateAsmConstraint(*Name, info)) {
Ted Kremenekd229d962008-04-24 16:36:38 +0000151 // FIXME: We temporarily return false
Anders Carlsson4ce42302007-11-27 04:11:28 +0000152 // so we can add more constraints as we hit it.
153 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekd229d962008-04-24 16:36:38 +0000154 return false;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000155 }
156 case '&': // early clobber.
157 break;
158 case 'r': // general register.
159 info = (ConstraintInfo)(info|CI_AllowsRegister);
160 break;
161 case 'm': // memory operand.
162 info = (ConstraintInfo)(info|CI_AllowsMemory);
163 break;
164 case 'g': // general register, memory operand or immediate integer.
165 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
166 break;
167 }
168
169 Name++;
170 }
171
172 return true;
173}
174
175bool TargetInfo::validateInputConstraint(const char *Name,
176 unsigned NumOutputs,
Chris Lattner0fb8d852008-03-08 08:24:01 +0000177 ConstraintInfo &info) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000178 while (*Name) {
179 switch (*Name) {
180 default:
181 // Check if we have a matching constraint
182 if (*Name >= '0' && *Name <= '9') {
183 unsigned i = *Name - '0';
184
185 // Check if matching constraint is out of bounds.
186 if (i >= NumOutputs)
187 return false;
Chris Lattner0fb8d852008-03-08 08:24:01 +0000188 } else if (!validateAsmConstraint(*Name, info)) {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000189 // FIXME: This assert is in place temporarily
190 // so we can add more constraints as we hit it.
191 // Eventually, an unknown constraint should just be treated as 'g'.
192 assert(0 && "Unknown input constraint type!");
193 }
Anders Carlsson333052c2007-12-08 19:32:57 +0000194 case '%': // commutative
195 // FIXME: Fail if % is used with the last operand.
196 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000197 case 'i': // immediate integer.
Anders Carlssonf8e50d92008-02-18 17:00:25 +0000198 case 'I':
Anders Carlssonbdb0e2b2008-03-09 06:02:02 +0000199 case 'n': // immediate integer with a known value.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000200 break;
201 case 'r': // general register.
202 info = (ConstraintInfo)(info|CI_AllowsRegister);
203 break;
204 case 'm': // memory operand.
205 info = (ConstraintInfo)(info|CI_AllowsMemory);
206 break;
207 case 'g': // general register, memory operand or immediate integer.
208 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
209 break;
210 }
211
212 Name++;
213 }
214
215 return true;
216}