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