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