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