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