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