Home | History | Annotate | Download | only in Basic
      1 //===--- TargetInfo.cpp - Information about Target machine ----------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file implements the TargetInfo and TargetInfoImpl interfaces.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "clang/Basic/TargetInfo.h"
     15 #include "clang/Basic/AddressSpaces.h"
     16 #include "clang/Basic/CharInfo.h"
     17 #include "clang/Basic/LangOptions.h"
     18 #include "llvm/ADT/APFloat.h"
     19 #include "llvm/ADT/STLExtras.h"
     20 #include "llvm/Support/ErrorHandling.h"
     21 #include <cstdlib>
     22 using namespace clang;
     23 
     24 static const LangAS::Map DefaultAddrSpaceMap = { 0 };
     25 
     26 // TargetInfo Constructor.
     27 TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
     28   // Set defaults.  Defaults are set for a 32-bit RISC platform, like PPC or
     29   // SPARC.  These should be overridden by concrete targets as needed.
     30   BigEndian = true;
     31   TLSSupported = true;
     32   NoAsmVariants = false;
     33   PointerWidth = PointerAlign = 32;
     34   BoolWidth = BoolAlign = 8;
     35   IntWidth = IntAlign = 32;
     36   LongWidth = LongAlign = 32;
     37   LongLongWidth = LongLongAlign = 64;
     38   SuitableAlign = 64;
     39   MinGlobalAlign = 0;
     40   HalfWidth = 16;
     41   HalfAlign = 16;
     42   FloatWidth = 32;
     43   FloatAlign = 32;
     44   DoubleWidth = 64;
     45   DoubleAlign = 64;
     46   LongDoubleWidth = 64;
     47   LongDoubleAlign = 64;
     48   LargeArrayMinWidth = 0;
     49   LargeArrayAlign = 0;
     50   MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
     51   MaxVectorAlign = 0;
     52   SizeType = UnsignedLong;
     53   PtrDiffType = SignedLong;
     54   IntMaxType = SignedLongLong;
     55   UIntMaxType = UnsignedLongLong;
     56   IntPtrType = SignedLong;
     57   WCharType = SignedInt;
     58   WIntType = SignedInt;
     59   Char16Type = UnsignedShort;
     60   Char32Type = UnsignedInt;
     61   Int64Type = SignedLongLong;
     62   SigAtomicType = SignedInt;
     63   ProcessIDType = SignedInt;
     64   UseSignedCharForObjCBool = true;
     65   UseBitFieldTypeAlignment = true;
     66   UseZeroLengthBitfieldAlignment = false;
     67   ZeroLengthBitfieldBoundary = 0;
     68   HalfFormat = &llvm::APFloat::IEEEhalf;
     69   FloatFormat = &llvm::APFloat::IEEEsingle;
     70   DoubleFormat = &llvm::APFloat::IEEEdouble;
     71   LongDoubleFormat = &llvm::APFloat::IEEEdouble;
     72   DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
     73                       "i64:64:64-f32:32:32-f64:64:64-n32";
     74   UserLabelPrefix = "_";
     75   MCountName = "mcount";
     76   RegParmMax = 0;
     77   SSERegParmMax = 0;
     78   HasAlignMac68kSupport = false;
     79 
     80   // Default to no types using fpret.
     81   RealTypeUsesObjCFPRet = 0;
     82 
     83   // Default to not using fp2ret for __Complex long double
     84   ComplexLongDoubleUsesFP2Ret = false;
     85 
     86   // Default to using the Itanium ABI.
     87   TheCXXABI.set(TargetCXXABI::GenericItanium);
     88 
     89   // Default to an empty address space map.
     90   AddrSpaceMap = &DefaultAddrSpaceMap;
     91 
     92   // Default to an unknown platform name.
     93   PlatformName = "unknown";
     94   PlatformMinVersion = VersionTuple();
     95 }
     96 
     97 // Out of line virtual dtor for TargetInfo.
     98 TargetInfo::~TargetInfo() {}
     99 
    100 /// getTypeName - Return the user string for the specified integer type enum.
    101 /// For example, SignedShort -> "short".
    102 const char *TargetInfo::getTypeName(IntType T) {
    103   switch (T) {
    104   default: llvm_unreachable("not an integer!");
    105   case SignedShort:      return "short";
    106   case UnsignedShort:    return "unsigned short";
    107   case SignedInt:        return "int";
    108   case UnsignedInt:      return "unsigned int";
    109   case SignedLong:       return "long int";
    110   case UnsignedLong:     return "long unsigned int";
    111   case SignedLongLong:   return "long long int";
    112   case UnsignedLongLong: return "long long unsigned int";
    113   }
    114 }
    115 
    116 /// getTypeConstantSuffix - Return the constant suffix for the specified
    117 /// integer type enum. For example, SignedLong -> "L".
    118 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
    119   switch (T) {
    120   default: llvm_unreachable("not an integer!");
    121   case SignedShort:
    122   case SignedInt:        return "";
    123   case SignedLong:       return "L";
    124   case SignedLongLong:   return "LL";
    125   case UnsignedShort:
    126   case UnsignedInt:      return "U";
    127   case UnsignedLong:     return "UL";
    128   case UnsignedLongLong: return "ULL";
    129   }
    130 }
    131 
    132 /// getTypeWidth - Return the width (in bits) of the specified integer type
    133 /// enum. For example, SignedInt -> getIntWidth().
    134 unsigned TargetInfo::getTypeWidth(IntType T) const {
    135   switch (T) {
    136   default: llvm_unreachable("not an integer!");
    137   case SignedShort:
    138   case UnsignedShort:    return getShortWidth();
    139   case SignedInt:
    140   case UnsignedInt:      return getIntWidth();
    141   case SignedLong:
    142   case UnsignedLong:     return getLongWidth();
    143   case SignedLongLong:
    144   case UnsignedLongLong: return getLongLongWidth();
    145   };
    146 }
    147 
    148 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
    149 /// enum. For example, SignedInt -> getIntAlign().
    150 unsigned TargetInfo::getTypeAlign(IntType T) const {
    151   switch (T) {
    152   default: llvm_unreachable("not an integer!");
    153   case SignedShort:
    154   case UnsignedShort:    return getShortAlign();
    155   case SignedInt:
    156   case UnsignedInt:      return getIntAlign();
    157   case SignedLong:
    158   case UnsignedLong:     return getLongAlign();
    159   case SignedLongLong:
    160   case UnsignedLongLong: return getLongLongAlign();
    161   };
    162 }
    163 
    164 /// isTypeSigned - Return whether an integer types is signed. Returns true if
    165 /// the type is signed; false otherwise.
    166 bool TargetInfo::isTypeSigned(IntType T) {
    167   switch (T) {
    168   default: llvm_unreachable("not an integer!");
    169   case SignedShort:
    170   case SignedInt:
    171   case SignedLong:
    172   case SignedLongLong:
    173     return true;
    174   case UnsignedShort:
    175   case UnsignedInt:
    176   case UnsignedLong:
    177   case UnsignedLongLong:
    178     return false;
    179   };
    180 }
    181 
    182 /// setForcedLangOptions - Set forced language options.
    183 /// Apply changes to the target information with respect to certain
    184 /// language options which change the target configuration.
    185 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
    186   if (Opts.NoBitFieldTypeAlign)
    187     UseBitFieldTypeAlignment = false;
    188   if (Opts.ShortWChar)
    189     WCharType = UnsignedShort;
    190 }
    191 
    192 //===----------------------------------------------------------------------===//
    193 
    194 
    195 static StringRef removeGCCRegisterPrefix(StringRef Name) {
    196   if (Name[0] == '%' || Name[0] == '#')
    197     Name = Name.substr(1);
    198 
    199   return Name;
    200 }
    201 
    202 /// isValidClobber - Returns whether the passed in string is
    203 /// a valid clobber in an inline asm statement. This is used by
    204 /// Sema.
    205 bool TargetInfo::isValidClobber(StringRef Name) const {
    206   return (isValidGCCRegisterName(Name) ||
    207 	  Name == "memory" || Name == "cc");
    208 }
    209 
    210 /// isValidGCCRegisterName - Returns whether the passed in string
    211 /// is a valid register name according to GCC. This is used by Sema for
    212 /// inline asm statements.
    213 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
    214   if (Name.empty())
    215     return false;
    216 
    217   const char * const *Names;
    218   unsigned NumNames;
    219 
    220   // Get rid of any register prefix.
    221   Name = removeGCCRegisterPrefix(Name);
    222 
    223   getGCCRegNames(Names, NumNames);
    224 
    225   // If we have a number it maps to an entry in the register name array.
    226   if (isDigit(Name[0])) {
    227     int n;
    228     if (!Name.getAsInteger(0, n))
    229       return n >= 0 && (unsigned)n < NumNames;
    230   }
    231 
    232   // Check register names.
    233   for (unsigned i = 0; i < NumNames; i++) {
    234     if (Name == Names[i])
    235       return true;
    236   }
    237 
    238   // Check any additional names that we have.
    239   const AddlRegName *AddlNames;
    240   unsigned NumAddlNames;
    241   getGCCAddlRegNames(AddlNames, NumAddlNames);
    242   for (unsigned i = 0; i < NumAddlNames; i++)
    243     for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
    244       if (!AddlNames[i].Names[j])
    245 	break;
    246       // Make sure the register that the additional name is for is within
    247       // the bounds of the register names from above.
    248       if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
    249 	return true;
    250   }
    251 
    252   // Now check aliases.
    253   const GCCRegAlias *Aliases;
    254   unsigned NumAliases;
    255 
    256   getGCCRegAliases(Aliases, NumAliases);
    257   for (unsigned i = 0; i < NumAliases; i++) {
    258     for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
    259       if (!Aliases[i].Aliases[j])
    260         break;
    261       if (Aliases[i].Aliases[j] == Name)
    262         return true;
    263     }
    264   }
    265 
    266   return false;
    267 }
    268 
    269 StringRef
    270 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
    271   assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
    272 
    273   // Get rid of any register prefix.
    274   Name = removeGCCRegisterPrefix(Name);
    275 
    276   const char * const *Names;
    277   unsigned NumNames;
    278 
    279   getGCCRegNames(Names, NumNames);
    280 
    281   // First, check if we have a number.
    282   if (isDigit(Name[0])) {
    283     int n;
    284     if (!Name.getAsInteger(0, n)) {
    285       assert(n >= 0 && (unsigned)n < NumNames &&
    286              "Out of bounds register number!");
    287       return Names[n];
    288     }
    289   }
    290 
    291   // Check any additional names that we have.
    292   const AddlRegName *AddlNames;
    293   unsigned NumAddlNames;
    294   getGCCAddlRegNames(AddlNames, NumAddlNames);
    295   for (unsigned i = 0; i < NumAddlNames; i++)
    296     for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
    297       if (!AddlNames[i].Names[j])
    298 	break;
    299       // Make sure the register that the additional name is for is within
    300       // the bounds of the register names from above.
    301       if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
    302 	return Name;
    303     }
    304 
    305   // Now check aliases.
    306   const GCCRegAlias *Aliases;
    307   unsigned NumAliases;
    308 
    309   getGCCRegAliases(Aliases, NumAliases);
    310   for (unsigned i = 0; i < NumAliases; i++) {
    311     for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
    312       if (!Aliases[i].Aliases[j])
    313         break;
    314       if (Aliases[i].Aliases[j] == Name)
    315         return Aliases[i].Register;
    316     }
    317   }
    318 
    319   return Name;
    320 }
    321 
    322 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
    323   const char *Name = Info.getConstraintStr().c_str();
    324   // An output constraint must start with '=' or '+'
    325   if (*Name != '=' && *Name != '+')
    326     return false;
    327 
    328   if (*Name == '+')
    329     Info.setIsReadWrite();
    330 
    331   Name++;
    332   while (*Name) {
    333     switch (*Name) {
    334     default:
    335       if (!validateAsmConstraint(Name, Info)) {
    336         // FIXME: We temporarily return false
    337         // so we can add more constraints as we hit it.
    338         // Eventually, an unknown constraint should just be treated as 'g'.
    339         return false;
    340       }
    341     case '&': // early clobber.
    342       break;
    343     case '%': // commutative.
    344       // FIXME: Check that there is a another register after this one.
    345       break;
    346     case 'r': // general register.
    347       Info.setAllowsRegister();
    348       break;
    349     case 'm': // memory operand.
    350     case 'o': // offsetable memory operand.
    351     case 'V': // non-offsetable memory operand.
    352     case '<': // autodecrement memory operand.
    353     case '>': // autoincrement memory operand.
    354       Info.setAllowsMemory();
    355       break;
    356     case 'g': // general register, memory operand or immediate integer.
    357     case 'X': // any operand.
    358       Info.setAllowsRegister();
    359       Info.setAllowsMemory();
    360       break;
    361     case ',': // multiple alternative constraint.  Pass it.
    362       // Handle additional optional '=' or '+' modifiers.
    363       if (Name[1] == '=' || Name[1] == '+')
    364         Name++;
    365       break;
    366     case '?': // Disparage slightly code.
    367     case '!': // Disparage severely.
    368     case '#': // Ignore as constraint.
    369     case '*': // Ignore for choosing register preferences.
    370       break;  // Pass them.
    371     }
    372 
    373     Name++;
    374   }
    375 
    376   // If a constraint allows neither memory nor register operands it contains
    377   // only modifiers. Reject it.
    378   return Info.allowsMemory() || Info.allowsRegister();
    379 }
    380 
    381 bool TargetInfo::resolveSymbolicName(const char *&Name,
    382                                      ConstraintInfo *OutputConstraints,
    383                                      unsigned NumOutputs,
    384                                      unsigned &Index) const {
    385   assert(*Name == '[' && "Symbolic name did not start with '['");
    386   Name++;
    387   const char *Start = Name;
    388   while (*Name && *Name != ']')
    389     Name++;
    390 
    391   if (!*Name) {
    392     // Missing ']'
    393     return false;
    394   }
    395 
    396   std::string SymbolicName(Start, Name - Start);
    397 
    398   for (Index = 0; Index != NumOutputs; ++Index)
    399     if (SymbolicName == OutputConstraints[Index].getName())
    400       return true;
    401 
    402   return false;
    403 }
    404 
    405 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
    406                                          unsigned NumOutputs,
    407                                          ConstraintInfo &Info) const {
    408   const char *Name = Info.ConstraintStr.c_str();
    409 
    410   while (*Name) {
    411     switch (*Name) {
    412     default:
    413       // Check if we have a matching constraint
    414       if (*Name >= '0' && *Name <= '9') {
    415         unsigned i = *Name - '0';
    416 
    417         // Check if matching constraint is out of bounds.
    418         if (i >= NumOutputs)
    419           return false;
    420 
    421         // A number must refer to an output only operand.
    422         if (OutputConstraints[i].isReadWrite())
    423           return false;
    424 
    425         // If the constraint is already tied, it must be tied to the
    426         // same operand referenced to by the number.
    427         if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
    428           return false;
    429 
    430         // The constraint should have the same info as the respective
    431         // output constraint.
    432         Info.setTiedOperand(i, OutputConstraints[i]);
    433       } else if (!validateAsmConstraint(Name, Info)) {
    434         // FIXME: This error return is in place temporarily so we can
    435         // add more constraints as we hit it.  Eventually, an unknown
    436         // constraint should just be treated as 'g'.
    437         return false;
    438       }
    439       break;
    440     case '[': {
    441       unsigned Index = 0;
    442       if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
    443         return false;
    444 
    445       // If the constraint is already tied, it must be tied to the
    446       // same operand referenced to by the number.
    447       if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
    448         return false;
    449 
    450       Info.setTiedOperand(Index, OutputConstraints[Index]);
    451       break;
    452     }
    453     case '%': // commutative
    454       // FIXME: Fail if % is used with the last operand.
    455       break;
    456     case 'i': // immediate integer.
    457     case 'n': // immediate integer with a known value.
    458       break;
    459     case 'I':  // Various constant constraints with target-specific meanings.
    460     case 'J':
    461     case 'K':
    462     case 'L':
    463     case 'M':
    464     case 'N':
    465     case 'O':
    466     case 'P':
    467       break;
    468     case 'r': // general register.
    469       Info.setAllowsRegister();
    470       break;
    471     case 'm': // memory operand.
    472     case 'o': // offsettable memory operand.
    473     case 'V': // non-offsettable memory operand.
    474     case '<': // autodecrement memory operand.
    475     case '>': // autoincrement memory operand.
    476       Info.setAllowsMemory();
    477       break;
    478     case 'g': // general register, memory operand or immediate integer.
    479     case 'X': // any operand.
    480       Info.setAllowsRegister();
    481       Info.setAllowsMemory();
    482       break;
    483     case 'E': // immediate floating point.
    484     case 'F': // immediate floating point.
    485     case 'p': // address operand.
    486       break;
    487     case ',': // multiple alternative constraint.  Ignore comma.
    488       break;
    489     case '?': // Disparage slightly code.
    490     case '!': // Disparage severely.
    491     case '#': // Ignore as constraint.
    492     case '*': // Ignore for choosing register preferences.
    493       break;  // Pass them.
    494     }
    495 
    496     Name++;
    497   }
    498 
    499   return true;
    500 }
    501 
    502 bool TargetCXXABI::tryParse(llvm::StringRef name) {
    503   const Kind unknown = static_cast<Kind>(-1);
    504   Kind kind = llvm::StringSwitch<Kind>(name)
    505     .Case("arm", GenericARM)
    506     .Case("ios", iOS)
    507     .Case("itanium", GenericItanium)
    508     .Case("microsoft", Microsoft)
    509     .Default(unknown);
    510   if (kind == unknown) return false;
    511 
    512   set(kind);
    513   return true;
    514 }
    515