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