LLVM API Documentation

DarwinTargetAsmInfo.cpp

Go to the documentation of this file.
00001 //===-- DarwinTargetAsmInfo.cpp - Darwin asm properties ---------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines target asm properties related what form asm statements
00011 // should take in general on Darwin-based targets
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Constants.h"
00016 #include "llvm/DerivedTypes.h"
00017 #include "llvm/Function.h"
00018 #include "llvm/GlobalVariable.h"
00019 #include "llvm/ADT/StringExtras.h"
00020 #include "llvm/Support/Mangler.h"
00021 #include "llvm/Target/DarwinTargetAsmInfo.h"
00022 #include "llvm/Target/TargetMachine.h"
00023 #include "llvm/Target/TargetData.h"
00024 
00025 using namespace llvm;
00026 
00027 DarwinTargetAsmInfo::DarwinTargetAsmInfo(const TargetMachine &TM) 
00028   : TargetAsmInfo(TM) {
00029 
00030   CStringSection_ = getUnnamedSection("\t.cstring",
00031                                 SectionFlags::Mergeable | SectionFlags::Strings);
00032   FourByteConstantSection = getUnnamedSection("\t.literal4\n",
00033                                               SectionFlags::Mergeable);
00034   EightByteConstantSection = getUnnamedSection("\t.literal8\n",
00035                                                SectionFlags::Mergeable);
00036 
00037   // Note: 16-byte constant section is subtarget specific and should be provided
00038   // there, if needed.
00039   SixteenByteConstantSection = 0;
00040 
00041   ReadOnlySection = getUnnamedSection("\t.const\n", SectionFlags::None);
00042 
00043   TextCoalSection =
00044     getNamedSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
00045                     SectionFlags::Code);
00046   ConstTextCoalSection = getNamedSection("\t__TEXT,__const_coal,coalesced",
00047                                          SectionFlags::None);
00048   ConstDataCoalSection = getNamedSection("\t__DATA,__const_coal,coalesced",
00049                                          SectionFlags::None);
00050   ConstDataSection = getUnnamedSection(".const_data", SectionFlags::None);
00051   DataCoalSection = getNamedSection("\t__DATA,__datacoal_nt,coalesced",
00052                                     SectionFlags::Writeable);
00053 }
00054 
00055 /// emitUsedDirectiveFor - On Darwin, internally linked data beginning with
00056 /// the PrivateGlobalPrefix or the LessPrivateGlobalPrefix does not have the
00057 /// directive emitted (this occurs in ObjC metadata).
00058 
00059 bool
00060 DarwinTargetAsmInfo::emitUsedDirectiveFor(const GlobalValue* GV,
00061                                           Mangler *Mang) const {
00062   if (GV==0)
00063     return false;
00064   if (GV->hasInternalLinkage() && !isa<Function>(GV) &&
00065       ((strlen(getPrivateGlobalPrefix()) != 0 &&
00066         Mang->getValueName(GV).substr(0,strlen(getPrivateGlobalPrefix())) ==
00067           getPrivateGlobalPrefix()) ||
00068        (strlen(getLessPrivateGlobalPrefix()) != 0 &&
00069         Mang->getValueName(GV).substr(0,strlen(getLessPrivateGlobalPrefix())) ==
00070           getLessPrivateGlobalPrefix())))
00071     return false;
00072   return true;
00073 }
00074 
00075 const Section*
00076 DarwinTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
00077   SectionKind::Kind Kind = SectionKindForGlobal(GV);
00078   bool isWeak = GV->mayBeOverridden();
00079   bool isNonStatic = TM.getRelocationModel() != Reloc::Static;
00080 
00081   switch (Kind) {
00082    case SectionKind::Text:
00083     if (isWeak)
00084       return TextCoalSection;
00085     else
00086       return TextSection;
00087    case SectionKind::Data:
00088    case SectionKind::ThreadData:
00089    case SectionKind::BSS:
00090    case SectionKind::ThreadBSS:
00091     if (cast<GlobalVariable>(GV)->isConstant())
00092       return (isWeak ? ConstDataCoalSection : ConstDataSection);
00093     else
00094       return (isWeak ? DataCoalSection : DataSection);
00095    case SectionKind::ROData:
00096     return (isWeak ? ConstDataCoalSection :
00097             (isNonStatic ? ConstDataSection : getReadOnlySection()));
00098    case SectionKind::RODataMergeStr:
00099     return (isWeak ?
00100             ConstTextCoalSection :
00101             MergeableStringSection(cast<GlobalVariable>(GV)));
00102    case SectionKind::RODataMergeConst:
00103     return (isWeak ?
00104             ConstDataCoalSection:
00105             MergeableConstSection(cast<GlobalVariable>(GV)));
00106    default:
00107     assert(0 && "Unsuported section kind for global");
00108   }
00109 
00110   // FIXME: Do we have any extra special weird cases?
00111   return NULL;
00112 }
00113 
00114 const Section*
00115 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
00116   const TargetData *TD = TM.getTargetData();
00117   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
00118   const Type *Type = cast<ConstantArray>(C)->getType()->getElementType();
00119 
00120   unsigned Size = TD->getABITypeSize(Type);
00121   if (Size) {
00122     unsigned Align = TD->getPreferredAlignment(GV);
00123     if (Align <= 32)
00124       return getCStringSection_();
00125   }
00126 
00127   return getReadOnlySection();
00128 }
00129 
00130 const Section*
00131 DarwinTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
00132   Constant *C = GV->getInitializer();
00133 
00134   return MergeableConstSection(C->getType());
00135 }
00136 
00137 inline const Section*
00138 DarwinTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
00139   const TargetData *TD = TM.getTargetData();
00140 
00141   unsigned Size = TD->getABITypeSize(Ty);
00142   if (Size == 4)
00143     return FourByteConstantSection;
00144   else if (Size == 8)
00145     return EightByteConstantSection;
00146   else if (Size == 16 && SixteenByteConstantSection)
00147     return SixteenByteConstantSection;
00148 
00149   return getReadOnlySection();
00150 }
00151 
00152 const Section*
00153 DarwinTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
00154   const Section* S = MergeableConstSection(Ty);
00155 
00156   // Handle weird special case, when compiling PIC stuff.
00157   if (S == getReadOnlySection() &&
00158       TM.getRelocationModel() != Reloc::Static)
00159     return ConstDataSection;
00160 
00161   return S;
00162 }
00163 
00164 std::string
00165 DarwinTargetAsmInfo::UniqueSectionForGlobal(const GlobalValue* GV,
00166                                                SectionKind::Kind kind) const {
00167   assert(0 && "Darwin does not use unique sections");
00168   return "";
00169 }



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.