Temporarily revert r60519. It was causing a bootstrap failure:
[oota-llvm.git] / lib / Target / ARM / ARMTargetAsmInfo.cpp
1 //===-- ARMTargetAsmInfo.cpp - ARM asm properties ---------------*- C++ -*-===//
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 contains the declarations of the ARMTargetAsmInfo properties.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ARMTargetAsmInfo.h"
15 #include "ARMTargetMachine.h"
16 #include <cstring>
17 #include <cctype>
18 using namespace llvm;
19
20
21 const char *const llvm::arm_asm_table[] = {
22                                       "{r0}", "r0",
23                                       "{r1}", "r1",
24                                       "{r2}", "r2",
25                                       "{r3}", "r3",
26                                       "{r4}", "r4",
27                                       "{r5}", "r5",
28                                       "{r6}", "r6",
29                                       "{r7}", "r7",
30                                       "{r8}", "r8",
31                                       "{r9}", "r9",
32                                       "{r10}", "r10",
33                                       "{r11}", "r11",
34                                       "{r12}", "r12",
35                                       "{r13}", "r13",
36                                       "{r14}", "r14",
37                                       "{lr}", "lr",
38                                       "{sp}", "sp",
39                                       "{ip}", "ip",
40                                       "{fp}", "fp",
41                                       "{sl}", "sl",
42                                       "{memory}", "memory",
43                                       "{cc}", "cc",
44                                       0,0};
45
46 ARMDarwinTargetAsmInfo::ARMDarwinTargetAsmInfo(const ARMTargetMachine &TM):
47   ARMTargetAsmInfo<DarwinTargetAsmInfo>(TM) {
48   Subtarget = &TM.getSubtarget<ARMSubtarget>();
49
50   GlobalPrefix = "_";
51   PrivateGlobalPrefix = "L";
52   LessPrivateGlobalPrefix = "l";
53   StringConstantPrefix = "\1LC";
54   BSSSection = 0;                       // no BSS section
55   ZeroDirective = "\t.space\t";
56   ZeroFillDirective = "\t.zerofill\t";  // Uses .zerofill
57   SetDirective = "\t.set\t";
58   WeakRefDirective = "\t.weak_reference\t";
59   HiddenDirective = "\t.private_extern\t";
60   ProtectedDirective = NULL;
61   JumpTableDataSection = ".const";
62   CStringSection = "\t.cstring";
63   HasDotTypeDotSizeDirective = false;
64   HasSingleParameterDotFile = false;
65   NeedsIndirectEncoding = true;
66   if (TM.getRelocationModel() == Reloc::Static) {
67     StaticCtorsSection = ".constructor";
68     StaticDtorsSection = ".destructor";
69   } else {
70     StaticCtorsSection = ".mod_init_func";
71     StaticDtorsSection = ".mod_term_func";
72   }
73
74   // In non-PIC modes, emit a special label before jump tables so that the
75   // linker can perform more accurate dead code stripping.
76   if (TM.getRelocationModel() != Reloc::PIC_) {
77     // Emit a local label that is preserved until the linker runs.
78     JumpTableSpecialLabelPrefix = "l";
79   }
80
81   NeedsSet = true;
82   DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
83   DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
84   DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
85   DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
86   DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
87   DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
88   DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
89   DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
90   DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
91   DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
92   DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
93 }
94
95 ARMELFTargetAsmInfo::ARMELFTargetAsmInfo(const ARMTargetMachine &TM):
96   ARMTargetAsmInfo<ELFTargetAsmInfo>(TM) {
97   Subtarget = &TM.getSubtarget<ARMSubtarget>();
98
99   NeedsSet = false;
100   HasLEB128 = true;
101   AbsoluteDebugSectionOffsets = true;
102   CStringSection = ".rodata.str";
103   PrivateGlobalPrefix = ".L";
104   WeakRefDirective = "\t.weak\t";
105   SetDirective = "\t.set\t";
106   DwarfRequiresFrameSection = false;
107   DwarfAbbrevSection =  "\t.section\t.debug_abbrev,\"\",%progbits";
108   DwarfInfoSection =    "\t.section\t.debug_info,\"\",%progbits";
109   DwarfLineSection =    "\t.section\t.debug_line,\"\",%progbits";
110   DwarfFrameSection =   "\t.section\t.debug_frame,\"\",%progbits";
111   DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",%progbits";
112   DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",%progbits";
113   DwarfStrSection =     "\t.section\t.debug_str,\"\",%progbits";
114   DwarfLocSection =     "\t.section\t.debug_loc,\"\",%progbits";
115   DwarfARangesSection = "\t.section\t.debug_aranges,\"\",%progbits";
116   DwarfRangesSection =  "\t.section\t.debug_ranges,\"\",%progbits";
117   DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",%progbits";
118
119   if (Subtarget->isAAPCS_ABI()) {
120     StaticCtorsSection = "\t.section .init_array,\"aw\",%init_array";
121     StaticDtorsSection = "\t.section .fini_array,\"aw\",%fini_array";
122   } else {
123     StaticCtorsSection = "\t.section .ctors,\"aw\",%progbits";
124     StaticDtorsSection = "\t.section .dtors,\"aw\",%progbits";
125   }
126 }
127
128 /// Count the number of comma-separated arguments.
129 /// Do not try to detect errors.
130 template <class BaseTAI>
131 unsigned ARMTargetAsmInfo<BaseTAI>::countArguments(const char* p) const {
132   unsigned count = 0;
133   while (*p && isspace(*p) && *p != '\n')
134     p++;
135   count++;
136   while (*p && *p!='\n' &&
137          strncmp(p, BaseTAI::CommentString,
138                  strlen(BaseTAI::CommentString))!=0) {
139     if (*p==',')
140       count++;
141     p++;
142   }
143   return count;
144 }
145
146 /// Count the length of a string enclosed in quote characters.
147 /// Do not try to detect errors.
148 template <class BaseTAI>
149 unsigned ARMTargetAsmInfo<BaseTAI>::countString(const char* p) const {
150   unsigned count = 0;
151   while (*p && isspace(*p) && *p!='\n')
152     p++;
153   if (!*p || *p != '\"')
154     return count;
155   while (*++p && *p != '\"')
156     count++;
157   return count;
158 }
159
160 /// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
161 template <class BaseTAI>
162 unsigned ARMTargetAsmInfo<BaseTAI>::getInlineAsmLength(const char *s) const {
163   // Make a lowercase-folded version of s for counting purposes.
164   char *q, *s_copy = (char *)malloc(strlen(s) + 1);
165   strcpy(s_copy, s);
166   for (q=s_copy; *q; q++)
167     *q = tolower(*q);
168   const char *Str = s_copy;
169
170   // Count the number of bytes in the asm.
171   bool atInsnStart = true;
172   bool inTextSection = true;
173   unsigned Length = 0;
174   for (; *Str; ++Str) {
175     if (atInsnStart) {
176       // Skip whitespace
177       while (*Str && isspace(*Str) && *Str != '\n')
178         Str++;
179       // Skip label
180       for (const char* p = Str; *p && !isspace(*p); p++)
181         if (*p == ':') {
182           Str = p+1;
183           while (*Str && isspace(*Str) && *Str != '\n')
184             Str++;
185           break;
186         }
187       // Ignore everything from comment char(s) to EOL
188       if (strncmp(Str, BaseTAI::CommentString, strlen(BaseTAI::CommentString))==-0)
189         atInsnStart = false;
190       // FIXME do something like the following for non-Darwin
191       else if (*Str == '.' && Subtarget->isTargetDarwin()) {
192         // Directive.
193         atInsnStart = false;
194
195         // Some change the section, but don't generate code.
196         if (strncmp(Str, ".literal4", strlen(".literal4"))==0 ||
197             strncmp(Str, ".literal8", strlen(".literal8"))==0 ||
198             strncmp(Str, ".const", strlen(".const"))==0 ||
199             strncmp(Str, ".constructor", strlen(".constructor"))==0 ||
200             strncmp(Str, ".cstring", strlen(".cstring"))==0 ||
201             strncmp(Str, ".data", strlen(".data"))==0 ||
202             strncmp(Str, ".destructor", strlen(".destructor"))==0 ||
203             strncmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
204             strncmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
205             strncmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
206             strncmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
207             strncmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
208             strncmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
209             strncmp(Str, ".static_data", strlen(".static_data"))==0 ||
210             strncmp(Str, ".section", strlen(".section"))==0 ||
211             strncmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
212             strncmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
213             strncmp(Str, ".dyld", strlen(".dyld"))==0 ||
214             strncmp(Str, ".const_data", strlen(".const_data"))==0 ||
215             strncmp(Str, ".objc", strlen(".objc"))==0 ||       //// many directives
216             strncmp(Str, ".static_const", strlen(".static_const"))==0)
217           inTextSection=false;
218         else if (strncmp(Str, ".text", strlen(".text"))==0)
219           inTextSection = true;
220         // Some can't really be handled without implementing significant pieces
221         // of an assembler.  Others require dynamic adjustment of block sizes in
222         // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
223         // instruction in there, and none of these are currently used in the kernel.
224         else if (strncmp(Str, ".macro", strlen(".macro"))==0 ||
225                  strncmp(Str, ".if", strlen(".if"))==0 ||
226                  strncmp(Str, ".align", strlen(".align"))==0 ||
227                  strncmp(Str, ".fill", strlen(".fill"))==0 ||
228                  strncmp(Str, ".space", strlen(".space"))==0 ||
229                  strncmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
230                  strncmp(Str, ".p2align", strlen(".p2align"))==0 ||
231                  strncmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
232                  strncmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
233                  strncmp(Str, ".align32", strlen(".p2align32"))==0 ||
234                  strncmp(Str, ".include", strlen(".include"))==0)
235           cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
236                    " constant pools (the assembler will tell you if this happens).\n";
237         // Some generate code, but this is only interesting in the text section.
238         else if (inTextSection) {
239           if (strncmp(Str, ".long", strlen(".long"))==0)
240             Length += 4*countArguments(Str+strlen(".long"));
241           else if (strncmp(Str, ".short", strlen(".short"))==0)
242             Length += 2*countArguments(Str+strlen(".short"));
243           else if (strncmp(Str, ".byte", strlen(".byte"))==0)
244             Length += 1*countArguments(Str+strlen(".byte"));
245           else if (strncmp(Str, ".single", strlen(".single"))==0)
246             Length += 4*countArguments(Str+strlen(".single"));
247           else if (strncmp(Str, ".double", strlen(".double"))==0)
248             Length += 8*countArguments(Str+strlen(".double"));
249           else if (strncmp(Str, ".quad", strlen(".quad"))==0)
250             Length += 16*countArguments(Str+strlen(".quad"));
251           else if (strncmp(Str, ".ascii", strlen(".ascii"))==0)
252             Length += countString(Str+strlen(".ascii"));
253           else if (strncmp(Str, ".asciz", strlen(".asciz"))==0)
254             Length += countString(Str+strlen(".asciz"))+1;
255         }
256       } else if (inTextSection) {
257         // An instruction
258         atInsnStart = false;
259         if (Subtarget->isThumb()) {
260           // BL and BLX <non-reg> are 4 bytes, all others 2.
261           if (strncmp(Str, "blx", strlen("blx"))==0) {
262             const char* p = Str+3;
263             while (*p && isspace(*p))
264               p++;
265             if (*p == 'r' || *p=='R')
266               Length += 2;    // BLX reg
267             else
268               Length += 4;    // BLX non-reg
269           } else if (strncmp(Str, "bl", strlen("bl"))==0)
270             Length += 4;    // BL
271           else
272             Length += 2;    // Thumb anything else
273         }
274         else
275           Length += 4;    // ARM
276       }
277     }
278     if (*Str == '\n' || *Str == BaseTAI::SeparatorChar)
279       atInsnStart = true;
280   }
281   free(s_copy);
282   return Length;
283 }
284
285 // Instantiate default implementation.
286 TEMPLATE_INSTANTIATION(class ARMTargetAsmInfo<TargetAsmInfo>);