blob: a4419682ef7cfab6155eca8998b68777b584c72a [file] [log] [blame]
Jim Laskey8e8de8f2006-09-07 22:05:02 +00001//===-- ARMTargetAsmInfo.cpp - ARM asm properties ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskey8e8de8f2006-09-07 22:05:02 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the declarations of the ARMTargetAsmInfo properties.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARMTargetAsmInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000015#include "ARMTargetMachine.h"
Dale Johannesen24fb52d2007-04-23 20:04:35 +000016#include <cstring>
17#include <cctype>
Jim Laskey8e8de8f2006-09-07 22:05:02 +000018using namespace llvm;
19
Anton Korobeynikov6381a132008-10-05 08:53:29 +000020
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000021const char *const llvm::arm_asm_table[] = {
Dan Gohmancfbb2f02008-03-25 21:45:14 +000022 "{r0}", "r0",
Lauro Ramos Venancio61718a62007-06-08 21:06:23 +000023 "{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
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000046ARMDarwinTargetAsmInfo::ARMDarwinTargetAsmInfo(const ARMTargetMachine &TM):
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000047 ARMTargetAsmInfo<DarwinTargetAsmInfo>(TM) {
Dan Gohman8f092252008-11-03 18:22:42 +000048 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000049
50 GlobalPrefix = "_";
51 PrivateGlobalPrefix = "L";
Dale Johannesenb2dfb892008-09-09 01:21:22 +000052 LessPrivateGlobalPrefix = "l";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000053 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";
Evan Chengc3ccc1a2008-12-06 02:00:55 +000059 WeakDefDirective = "\t.weak_definition ";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000060 HiddenDirective = "\t.private_extern\t";
61 ProtectedDirective = NULL;
62 JumpTableDataSection = ".const";
63 CStringSection = "\t.cstring";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000064 HasDotTypeDotSizeDirective = false;
Rafael Espindola952b8392008-12-03 11:01:37 +000065 HasSingleParameterDotFile = false;
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000066 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.
Evan Chengb13bafe2009-06-18 20:37:15 +000077 // Emit a local label that is preserved until the linker runs.
78 // We do not check the relocation model here since it can be overridden
79 // later.
80 JumpTableSpecialLabelPrefix = "l";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000081
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
96ARMELFTargetAsmInfo::ARMELFTargetAsmInfo(const ARMTargetMachine &TM):
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000097 ARMTargetAsmInfo<ELFTargetAsmInfo>(TM) {
Dan Gohman8f092252008-11-03 18:22:42 +000098 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000099
100 NeedsSet = false;
101 HasLEB128 = true;
102 AbsoluteDebugSectionOffsets = true;
103 CStringSection = ".rodata.str";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000104 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 }
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000127}
128
Dale Johannesen67cf5612007-05-02 01:02:40 +0000129/// Count the number of comma-separated arguments.
130/// Do not try to detect errors.
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000131template <class BaseTAI>
132unsigned ARMTargetAsmInfo<BaseTAI>::countArguments(const char* p) const {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000133 unsigned count = 0;
134 while (*p && isspace(*p) && *p != '\n')
135 p++;
136 count++;
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000137 while (*p && *p!='\n' &&
138 strncmp(p, BaseTAI::CommentString,
139 strlen(BaseTAI::CommentString))!=0) {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000140 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.
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000149template <class BaseTAI>
150unsigned ARMTargetAsmInfo<BaseTAI>::countString(const char* p) const {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000151 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;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000159}
Dale Johannesen86501992007-04-29 19:17:45 +0000160
161/// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000162template <class BaseTAI>
163unsigned ARMTargetAsmInfo<BaseTAI>::getInlineAsmLength(const char *s) const {
Dale Johannesenca4571e2007-10-25 21:54:43 +0000164 // 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
Dale Johannesen86501992007-04-29 19:17:45 +0000171 // Count the number of bytes in the asm.
172 bool atInsnStart = true;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000173 bool inTextSection = true;
Dale Johannesen86501992007-04-29 19:17:45 +0000174 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;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000184 while (*Str && isspace(*Str) && *Str != '\n')
185 Str++;
Dale Johannesen86501992007-04-29 19:17:45 +0000186 break;
187 }
Chris Lattnereda2ec32009-03-11 16:14:25 +0000188
189 if (*Str == 0) break;
190
Dale Johannesen86501992007-04-29 19:17:45 +0000191 // Ignore everything from comment char(s) to EOL
Chris Lattnereda2ec32009-03-11 16:14:25 +0000192 if (strncmp(Str, BaseTAI::CommentString,
193 strlen(BaseTAI::CommentString)) == 0)
Dale Johannesen86501992007-04-29 19:17:45 +0000194 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000195 // FIXME do something like the following for non-Darwin
196 else if (*Str == '.' && Subtarget->isTargetDarwin()) {
197 // Directive.
198 atInsnStart = false;
Dale Johannesenca4571e2007-10-25 21:54:43 +0000199
Dale Johannesen67cf5612007-05-02 01:02:40 +0000200 // Some change the section, but don't generate code.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000201 if (strncmp(Str, ".literal4", strlen(".literal4"))==0 ||
202 strncmp(Str, ".literal8", strlen(".literal8"))==0 ||
203 strncmp(Str, ".const", strlen(".const"))==0 ||
204 strncmp(Str, ".constructor", strlen(".constructor"))==0 ||
205 strncmp(Str, ".cstring", strlen(".cstring"))==0 ||
206 strncmp(Str, ".data", strlen(".data"))==0 ||
207 strncmp(Str, ".destructor", strlen(".destructor"))==0 ||
208 strncmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
209 strncmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
210 strncmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
211 strncmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
212 strncmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
213 strncmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
214 strncmp(Str, ".static_data", strlen(".static_data"))==0 ||
215 strncmp(Str, ".section", strlen(".section"))==0 ||
216 strncmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
217 strncmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
218 strncmp(Str, ".dyld", strlen(".dyld"))==0 ||
219 strncmp(Str, ".const_data", strlen(".const_data"))==0 ||
220 strncmp(Str, ".objc", strlen(".objc"))==0 || //// many directives
221 strncmp(Str, ".static_const", strlen(".static_const"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000222 inTextSection=false;
Dale Johannesenca4571e2007-10-25 21:54:43 +0000223 else if (strncmp(Str, ".text", strlen(".text"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000224 inTextSection = true;
225 // Some can't really be handled without implementing significant pieces
226 // of an assembler. Others require dynamic adjustment of block sizes in
227 // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
228 // instruction in there, and none of these are currently used in the kernel.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000229 else if (strncmp(Str, ".macro", strlen(".macro"))==0 ||
230 strncmp(Str, ".if", strlen(".if"))==0 ||
231 strncmp(Str, ".align", strlen(".align"))==0 ||
232 strncmp(Str, ".fill", strlen(".fill"))==0 ||
233 strncmp(Str, ".space", strlen(".space"))==0 ||
234 strncmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
235 strncmp(Str, ".p2align", strlen(".p2align"))==0 ||
236 strncmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
237 strncmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
238 strncmp(Str, ".align32", strlen(".p2align32"))==0 ||
239 strncmp(Str, ".include", strlen(".include"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000240 cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
241 " constant pools (the assembler will tell you if this happens).\n";
242 // Some generate code, but this is only interesting in the text section.
243 else if (inTextSection) {
Dale Johannesenca4571e2007-10-25 21:54:43 +0000244 if (strncmp(Str, ".long", strlen(".long"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000245 Length += 4*countArguments(Str+strlen(".long"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000246 else if (strncmp(Str, ".short", strlen(".short"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000247 Length += 2*countArguments(Str+strlen(".short"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000248 else if (strncmp(Str, ".byte", strlen(".byte"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000249 Length += 1*countArguments(Str+strlen(".byte"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000250 else if (strncmp(Str, ".single", strlen(".single"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000251 Length += 4*countArguments(Str+strlen(".single"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000252 else if (strncmp(Str, ".double", strlen(".double"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000253 Length += 8*countArguments(Str+strlen(".double"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000254 else if (strncmp(Str, ".quad", strlen(".quad"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000255 Length += 16*countArguments(Str+strlen(".quad"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000256 else if (strncmp(Str, ".ascii", strlen(".ascii"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000257 Length += countString(Str+strlen(".ascii"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000258 else if (strncmp(Str, ".asciz", strlen(".asciz"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000259 Length += countString(Str+strlen(".asciz"))+1;
260 }
261 } else if (inTextSection) {
Dale Johannesen86501992007-04-29 19:17:45 +0000262 // An instruction
263 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000264 if (Subtarget->isThumb()) {
Dale Johannesen86501992007-04-29 19:17:45 +0000265 // BL and BLX <non-reg> are 4 bytes, all others 2.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000266 if (strncmp(Str, "blx", strlen("blx"))==0) {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000267 const char* p = Str+3;
268 while (*p && isspace(*p))
269 p++;
270 if (*p == 'r' || *p=='R')
271 Length += 2; // BLX reg
Dale Johannesen86501992007-04-29 19:17:45 +0000272 else
Dale Johannesen67cf5612007-05-02 01:02:40 +0000273 Length += 4; // BLX non-reg
Dale Johannesenca4571e2007-10-25 21:54:43 +0000274 } else if (strncmp(Str, "bl", strlen("bl"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000275 Length += 4; // BL
276 else
Dale Johannesen86501992007-04-29 19:17:45 +0000277 Length += 2; // Thumb anything else
278 }
279 else
280 Length += 4; // ARM
281 }
282 }
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000283 if (*Str == '\n' || *Str == BaseTAI::SeparatorChar)
Dale Johannesen86501992007-04-29 19:17:45 +0000284 atInsnStart = true;
285 }
Dale Johannesenca4571e2007-10-25 21:54:43 +0000286 free(s_copy);
Dale Johannesen86501992007-04-29 19:17:45 +0000287 return Length;
288}
Anton Korobeynikov6381a132008-10-05 08:53:29 +0000289
290// Instantiate default implementation.
291TEMPLATE_INSTANTIATION(class ARMTargetAsmInfo<TargetAsmInfo>);