blob: 259a6170cd82551b2f666f50d495dd2ce4603380 [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 Lattnere0efe2e2008-05-09 05:47:41 +000027 LongWidth = LongAlign = 32;
Chris Lattner85970f32008-05-08 05:58:21 +000028 IntWidth = IntAlign = 32;
Nate Begemand5e35f82008-04-18 17:17:24 +000029 DoubleWidth = 64;
30 DoubleAlign = 32;
Chris Lattner3b74cac2008-03-08 08:59:43 +000031 FloatFormat = &llvm::APFloat::IEEEsingle;
32 DoubleFormat = &llvm::APFloat::IEEEdouble;
33 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
34}
35
Chris Lattner0fb8d852008-03-08 08:24:01 +000036// Out of line virtual dtor for TargetInfo.
37TargetInfo::~TargetInfo() {}
Chris Lattner4b009652007-07-25 00:24:17 +000038
Chris Lattner858eece2007-09-22 18:29:59 +000039//===----------------------------------------------------------------------===//
Chris Lattner858eece2007-09-22 18:29:59 +000040
Chris Lattner4b009652007-07-25 00:24:17 +000041
Chris Lattnerfc457002008-03-05 01:18:20 +000042static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlsson74385982008-02-06 00:11:32 +000043 if (Name[0] == '%' || Name[0] == '#')
44 Name++;
45}
46
Anders Carlsson7dd1c952007-11-24 23:38:12 +000047/// isValidGCCRegisterName - Returns whether the passed in string
48/// is a valid register name according to GCC. This is used by Sema for
49/// inline asm statements.
50bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson49dadd62007-11-25 00:25:21 +000051 const char * const *Names;
52 unsigned NumNames;
53
54 // Get rid of any register prefix.
Anders Carlsson74385982008-02-06 00:11:32 +000055 removeGCCRegisterPrefix(Name);
56
Anders Carlsson49dadd62007-11-25 00:25:21 +000057
58 if (strcmp(Name, "memory") == 0 ||
59 strcmp(Name, "cc") == 0)
60 return true;
61
Chris Lattner0fb8d852008-03-08 08:24:01 +000062 getGCCRegNames(Names, NumNames);
Anders Carlsson49dadd62007-11-25 00:25:21 +000063
64 // If we have a number it maps to an entry in the register name array.
65 if (isdigit(Name[0])) {
66 char *End;
67 int n = (int)strtol(Name, &End, 0);
68 if (*End == 0)
69 return n >= 0 && (unsigned)n < NumNames;
70 }
71
72 // Check register names.
73 for (unsigned i = 0; i < NumNames; i++) {
74 if (strcmp(Name, Names[i]) == 0)
75 return true;
76 }
77
78 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +000079 const GCCRegAlias *Aliases;
Anders Carlsson49dadd62007-11-25 00:25:21 +000080 unsigned NumAliases;
81
Chris Lattner0fb8d852008-03-08 08:24:01 +000082 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson49dadd62007-11-25 00:25:21 +000083 for (unsigned i = 0; i < NumAliases; i++) {
84 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
85 if (!Aliases[i].Aliases[j])
86 break;
87 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
88 return true;
89 }
90 }
91
Anders Carlsson7dd1c952007-11-24 23:38:12 +000092 return false;
93}
Anders Carlsson4ce42302007-11-27 04:11:28 +000094
Chris Lattner0fb8d852008-03-08 08:24:01 +000095const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +000096 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
97
Anders Carlsson74385982008-02-06 00:11:32 +000098 removeGCCRegisterPrefix(Name);
Anders Carlsson3fb39b12008-02-05 23:30:20 +000099
Anders Carlsson4ce42302007-11-27 04:11:28 +0000100 const char * const *Names;
101 unsigned NumNames;
102
Chris Lattner0fb8d852008-03-08 08:24:01 +0000103 getGCCRegNames(Names, NumNames);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000104
105 // First, check if we have a number.
106 if (isdigit(Name[0])) {
107 char *End;
108 int n = (int)strtol(Name, &End, 0);
109 if (*End == 0) {
110 assert(n >= 0 && (unsigned)n < NumNames &&
111 "Out of bounds register number!");
112 return Names[n];
113 }
114 }
115
116 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000117 const GCCRegAlias *Aliases;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000118 unsigned NumAliases;
119
Chris Lattner0fb8d852008-03-08 08:24:01 +0000120 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000121 for (unsigned i = 0; i < NumAliases; i++) {
122 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
123 if (!Aliases[i].Aliases[j])
124 break;
125 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
126 return Aliases[i].Register;
127 }
128 }
129
130 return Name;
131}
132
133bool TargetInfo::validateOutputConstraint(const char *Name,
134 ConstraintInfo &info) const
135{
136 // An output constraint must start with '=' or '+'
137 if (*Name != '=' && *Name != '+')
138 return false;
139
140 if (*Name == '+')
141 info = CI_ReadWrite;
142 else
143 info = CI_None;
144
145 Name++;
146 while (*Name) {
147 switch (*Name) {
148 default:
Chris Lattner0fb8d852008-03-08 08:24:01 +0000149 if (!validateAsmConstraint(*Name, info)) {
Ted Kremenekd229d962008-04-24 16:36:38 +0000150 // FIXME: We temporarily return false
Anders Carlsson4ce42302007-11-27 04:11:28 +0000151 // so we can add more constraints as we hit it.
152 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekd229d962008-04-24 16:36:38 +0000153 return false;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000154 }
155 case '&': // early clobber.
156 break;
157 case 'r': // general register.
158 info = (ConstraintInfo)(info|CI_AllowsRegister);
159 break;
160 case 'm': // memory operand.
161 info = (ConstraintInfo)(info|CI_AllowsMemory);
162 break;
163 case 'g': // general register, memory operand or immediate integer.
164 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
165 break;
166 }
167
168 Name++;
169 }
170
171 return true;
172}
173
174bool TargetInfo::validateInputConstraint(const char *Name,
175 unsigned NumOutputs,
Chris Lattner0fb8d852008-03-08 08:24:01 +0000176 ConstraintInfo &info) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000177 while (*Name) {
178 switch (*Name) {
179 default:
180 // Check if we have a matching constraint
181 if (*Name >= '0' && *Name <= '9') {
182 unsigned i = *Name - '0';
183
184 // Check if matching constraint is out of bounds.
185 if (i >= NumOutputs)
186 return false;
Chris Lattner0fb8d852008-03-08 08:24:01 +0000187 } else if (!validateAsmConstraint(*Name, info)) {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000188 // FIXME: This assert is in place temporarily
189 // so we can add more constraints as we hit it.
190 // Eventually, an unknown constraint should just be treated as 'g'.
191 assert(0 && "Unknown input constraint type!");
192 }
Anders Carlsson333052c2007-12-08 19:32:57 +0000193 case '%': // commutative
194 // FIXME: Fail if % is used with the last operand.
195 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000196 case 'i': // immediate integer.
Anders Carlssonf8e50d92008-02-18 17:00:25 +0000197 case 'I':
Anders Carlssonbdb0e2b2008-03-09 06:02:02 +0000198 case 'n': // immediate integer with a known value.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000199 break;
200 case 'r': // general register.
201 info = (ConstraintInfo)(info|CI_AllowsRegister);
202 break;
203 case 'm': // memory operand.
204 info = (ConstraintInfo)(info|CI_AllowsMemory);
205 break;
206 case 'g': // general register, memory operand or immediate integer.
207 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
208 break;
209 }
210
211 Name++;
212 }
213
214 return true;
215}