LLVM API Documentation

ARMTargetAsmInfo.cpp

Go to the documentation of this file.
00001 //===-- ARMTargetAsmInfo.cpp - ARM 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 contains the declarations of the ARMTargetAsmInfo properties.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "ARMTargetAsmInfo.h"
00015 #include "ARMTargetMachine.h"
00016 #include <cstring>
00017 #include <cctype>
00018 using namespace llvm;
00019 
00020 
00021 const char *const llvm::arm_asm_table[] = {
00022                                       "{r0}", "r0",
00023                                       "{r1}", "r1",
00024                                       "{r2}", "r2",
00025                                       "{r3}", "r3",
00026                                       "{r4}", "r4",
00027                                       "{r5}", "r5",
00028                                       "{r6}", "r6",
00029                                       "{r7}", "r7",
00030                                       "{r8}", "r8",
00031                                       "{r9}", "r9",
00032                                       "{r10}", "r10",
00033                                       "{r11}", "r11",
00034                                       "{r12}", "r12",
00035                                       "{r13}", "r13",
00036                                       "{r14}", "r14",
00037                                       "{lr}", "lr",
00038                                       "{sp}", "sp",
00039                                       "{ip}", "ip",
00040                                       "{fp}", "fp",
00041                                       "{sl}", "sl",
00042                                       "{memory}", "memory",
00043                                       "{cc}", "cc",
00044                                       0,0};
00045 
00046 ARMDarwinTargetAsmInfo::ARMDarwinTargetAsmInfo(const ARMTargetMachine &TM):
00047   ARMTargetAsmInfo<DarwinTargetAsmInfo>(TM) {
00048   Subtarget = &TM.getSubtarget<ARMSubtarget>();
00049 
00050   GlobalPrefix = "_";
00051   PrivateGlobalPrefix = "L";
00052   LessPrivateGlobalPrefix = "l";
00053   StringConstantPrefix = "\1LC";
00054   BSSSection = 0;                       // no BSS section
00055   ZeroDirective = "\t.space\t";
00056   ZeroFillDirective = "\t.zerofill\t";  // Uses .zerofill
00057   SetDirective = "\t.set\t";
00058   WeakRefDirective = "\t.weak_reference\t";
00059   WeakDefDirective = "\t.weak_definition ";
00060   HiddenDirective = "\t.private_extern\t";
00061   ProtectedDirective = NULL;
00062   JumpTableDataSection = ".const";
00063   CStringSection = "\t.cstring";
00064   HasDotTypeDotSizeDirective = false;
00065   HasSingleParameterDotFile = false;
00066   NeedsIndirectEncoding = true;
00067   if (TM.getRelocationModel() == Reloc::Static) {
00068     StaticCtorsSection = ".constructor";
00069     StaticDtorsSection = ".destructor";
00070   } else {
00071     StaticCtorsSection = ".mod_init_func";
00072     StaticDtorsSection = ".mod_term_func";
00073   }
00074 
00075   // In non-PIC modes, emit a special label before jump tables so that the
00076   // linker can perform more accurate dead code stripping.
00077   if (TM.getRelocationModel() != Reloc::PIC_) {
00078     // Emit a local label that is preserved until the linker runs.
00079     JumpTableSpecialLabelPrefix = "l";
00080   }
00081 
00082   NeedsSet = true;
00083   DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
00084   DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
00085   DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
00086   DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
00087   DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
00088   DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
00089   DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
00090   DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
00091   DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
00092   DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
00093   DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
00094 }
00095 
00096 ARMELFTargetAsmInfo::ARMELFTargetAsmInfo(const ARMTargetMachine &TM):
00097   ARMTargetAsmInfo<ELFTargetAsmInfo>(TM) {
00098   Subtarget = &TM.getSubtarget<ARMSubtarget>();
00099 
00100   NeedsSet = false;
00101   HasLEB128 = true;
00102   AbsoluteDebugSectionOffsets = true;
00103   CStringSection = ".rodata.str";
00104   PrivateGlobalPrefix = ".L";
00105   WeakRefDirective = "\t.weak\t";
00106   SetDirective = "\t.set\t";
00107   DwarfRequiresFrameSection = false;
00108   DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"\",%progbits";
00109   DwarfInfoSection =    "\t.section\t.debug_info,\"\",%progbits";
00110   DwarfLineSection =    "\t.section\t.debug_line,\"\",%progbits";
00111   DwarfFrameSection =   "\t.section\t.debug_frame,\"\",%progbits";
00112   DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",%progbits";
00113   DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",%progbits";
00114   DwarfStrSection =     "\t.section\t.debug_str,\"\",%progbits";
00115   DwarfLocSection =     "\t.section\t.debug_loc,\"\",%progbits";
00116   DwarfARangesSection = "\t.section\t.debug_aranges,\"\",%progbits";
00117   DwarfRangesSection =  "\t.section\t.debug_ranges,\"\",%progbits";
00118   DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",%progbits";
00119 
00120   if (Subtarget->isAAPCS_ABI()) {
00121     StaticCtorsSection = "\t.section .init_array,\"aw\",%init_array";
00122     StaticDtorsSection = "\t.section .fini_array,\"aw\",%fini_array";
00123   } else {
00124     StaticCtorsSection = "\t.section .ctors,\"aw\",%progbits";
00125     StaticDtorsSection = "\t.section .dtors,\"aw\",%progbits";
00126   }
00127 }
00128 
00129 /// Count the number of comma-separated arguments.
00130 /// Do not try to detect errors.
00131 template <class BaseTAI>
00132 unsigned ARMTargetAsmInfo<BaseTAI>::countArguments(const char* p) const {
00133   unsigned count = 0;
00134   while (*p && isspace(*p) && *p != '\n')
00135     p++;
00136   count++;
00137   while (*p && *p!='\n' &&
00138          strncmp(p, BaseTAI::CommentString,
00139                  strlen(BaseTAI::CommentString))!=0) {
00140     if (*p==',')
00141       count++;
00142     p++;
00143   }
00144   return count;
00145 }
00146 
00147 /// Count the length of a string enclosed in quote characters.
00148 /// Do not try to detect errors.
00149 template <class BaseTAI>
00150 unsigned ARMTargetAsmInfo<BaseTAI>::countString(const char* p) const {
00151   unsigned count = 0;
00152   while (*p && isspace(*p) && *p!='\n')
00153     p++;
00154   if (!*p || *p != '\"')
00155     return count;
00156   while (*++p && *p != '\"')
00157     count++;
00158   return count;
00159 }
00160 
00161 /// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
00162 template <class BaseTAI>
00163 unsigned ARMTargetAsmInfo<BaseTAI>::getInlineAsmLength(const char *s) const {
00164   // Make a lowercase-folded version of s for counting purposes.
00165   char *q, *s_copy = (char *)malloc(strlen(s) + 1);
00166   strcpy(s_copy, s);
00167   for (q=s_copy; *q; q++)
00168     *q = tolower(*q);
00169   const char *Str = s_copy;
00170 
00171   // Count the number of bytes in the asm.
00172   bool atInsnStart = true;
00173   bool inTextSection = true;
00174   unsigned Length = 0;
00175   for (; *Str; ++Str) {
00176     if (atInsnStart) {
00177       // Skip whitespace
00178       while (*Str && isspace(*Str) && *Str != '\n')
00179         Str++;
00180       // Skip label
00181       for (const char* p = Str; *p && !isspace(*p); p++)
00182         if (*p == ':') {
00183           Str = p+1;
00184           while (*Str && isspace(*Str) && *Str != '\n')
00185             Str++;
00186           break;
00187         }
00188       // Ignore everything from comment char(s) to EOL
00189       if (strncmp(Str, BaseTAI::CommentString, strlen(BaseTAI::CommentString))==-0)
00190         atInsnStart = false;
00191       // FIXME do something like the following for non-Darwin
00192       else if (*Str == '.' && Subtarget->isTargetDarwin()) {
00193         // Directive.
00194         atInsnStart = false;
00195 
00196         // Some change the section, but don't generate code.
00197         if (strncmp(Str, ".literal4", strlen(".literal4"))==0 ||
00198             strncmp(Str, ".literal8", strlen(".literal8"))==0 ||
00199             strncmp(Str, ".const", strlen(".const"))==0 ||
00200             strncmp(Str, ".constructor", strlen(".constructor"))==0 ||
00201             strncmp(Str, ".cstring", strlen(".cstring"))==0 ||
00202             strncmp(Str, ".data", strlen(".data"))==0 ||
00203             strncmp(Str, ".destructor", strlen(".destructor"))==0 ||
00204             strncmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
00205             strncmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
00206             strncmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
00207             strncmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
00208             strncmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
00209             strncmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
00210             strncmp(Str, ".static_data", strlen(".static_data"))==0 ||
00211             strncmp(Str, ".section", strlen(".section"))==0 ||
00212             strncmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
00213             strncmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
00214             strncmp(Str, ".dyld", strlen(".dyld"))==0 ||
00215             strncmp(Str, ".const_data", strlen(".const_data"))==0 ||
00216             strncmp(Str, ".objc", strlen(".objc"))==0 ||       //// many directives
00217             strncmp(Str, ".static_const", strlen(".static_const"))==0)
00218           inTextSection=false;
00219         else if (strncmp(Str, ".text", strlen(".text"))==0)
00220           inTextSection = true;
00221         // Some can't really be handled without implementing significant pieces
00222         // of an assembler.  Others require dynamic adjustment of block sizes in
00223         // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
00224         // instruction in there, and none of these are currently used in the kernel.
00225         else if (strncmp(Str, ".macro", strlen(".macro"))==0 ||
00226                  strncmp(Str, ".if", strlen(".if"))==0 ||
00227                  strncmp(Str, ".align", strlen(".align"))==0 ||
00228                  strncmp(Str, ".fill", strlen(".fill"))==0 ||
00229                  strncmp(Str, ".space", strlen(".space"))==0 ||
00230                  strncmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
00231                  strncmp(Str, ".p2align", strlen(".p2align"))==0 ||
00232                  strncmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
00233                  strncmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
00234                  strncmp(Str, ".align32", strlen(".p2align32"))==0 ||
00235                  strncmp(Str, ".include", strlen(".include"))==0)
00236           cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
00237                    " constant pools (the assembler will tell you if this happens).\n";
00238         // Some generate code, but this is only interesting in the text section.
00239         else if (inTextSection) {
00240           if (strncmp(Str, ".long", strlen(".long"))==0)
00241             Length += 4*countArguments(Str+strlen(".long"));
00242           else if (strncmp(Str, ".short", strlen(".short"))==0)
00243             Length += 2*countArguments(Str+strlen(".short"));
00244           else if (strncmp(Str, ".byte", strlen(".byte"))==0)
00245             Length += 1*countArguments(Str+strlen(".byte"));
00246           else if (strncmp(Str, ".single", strlen(".single"))==0)
00247             Length += 4*countArguments(Str+strlen(".single"));
00248           else if (strncmp(Str, ".double", strlen(".double"))==0)
00249             Length += 8*countArguments(Str+strlen(".double"));
00250           else if (strncmp(Str, ".quad", strlen(".quad"))==0)
00251             Length += 16*countArguments(Str+strlen(".quad"));
00252           else if (strncmp(Str, ".ascii", strlen(".ascii"))==0)
00253             Length += countString(Str+strlen(".ascii"));
00254           else if (strncmp(Str, ".asciz", strlen(".asciz"))==0)
00255             Length += countString(Str+strlen(".asciz"))+1;
00256         }
00257       } else if (inTextSection) {
00258         // An instruction
00259         atInsnStart = false;
00260         if (Subtarget->isThumb()) {
00261           // BL and BLX <non-reg> are 4 bytes, all others 2.
00262           if (strncmp(Str, "blx", strlen("blx"))==0) {
00263             const char* p = Str+3;
00264             while (*p && isspace(*p))
00265               p++;
00266             if (*p == 'r' || *p=='R')
00267               Length += 2;    // BLX reg
00268             else
00269               Length += 4;    // BLX non-reg
00270           } else if (strncmp(Str, "bl", strlen("bl"))==0)
00271             Length += 4;    // BL
00272           else
00273             Length += 2;    // Thumb anything else
00274         }
00275         else
00276           Length += 4;    // ARM
00277       }
00278     }
00279     if (*Str == '\n' || *Str == BaseTAI::SeparatorChar)
00280       atInsnStart = true;
00281   }
00282   free(s_copy);
00283   return Length;
00284 }
00285 
00286 // Instantiate default implementation.
00287 TEMPLATE_INSTANTIATION(class ARMTargetAsmInfo<TargetAsmInfo>);



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