blob: e0c81d61cbd24ece5e563fa2a9912238b7a85242 [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";
59 HiddenDirective = "\t.private_extern\t";
60 ProtectedDirective = NULL;
61 JumpTableDataSection = ".const";
62 CStringSection = "\t.cstring";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000063 HasDotTypeDotSizeDirective = false;
Rafael Espindola952b8392008-12-03 11:01:37 +000064 HasSingleParameterDotFile = false;
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000065 NeedsIndirectEncoding = true;
66 if (TM.getRelocationModel() == Reloc::Static) {
67 StaticCtorsSection = ".constructor";
68 StaticDtorsSection = ".destructor";
69 } else {
70 StaticCtorsSection = ".mod_init_func";
71 StaticDtorsSection = ".mod_term_func";
72 }
73
74 // In non-PIC modes, emit a special label before jump tables so that the
75 // linker can perform more accurate dead code stripping.
76 if (TM.getRelocationModel() != Reloc::PIC_) {
77 // Emit a local label that is preserved until the linker runs.
78 JumpTableSpecialLabelPrefix = "l";
79 }
80
81 NeedsSet = true;
82 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
83 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
84 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
85 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
86 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
87 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
88 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
89 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
90 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
91 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
92 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
93}
94
95ARMELFTargetAsmInfo::ARMELFTargetAsmInfo(const ARMTargetMachine &TM):
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000096 ARMTargetAsmInfo<ELFTargetAsmInfo>(TM) {
Dan Gohman8f092252008-11-03 18:22:42 +000097 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000098
99 NeedsSet = false;
100 HasLEB128 = true;
101 AbsoluteDebugSectionOffsets = true;
102 CStringSection = ".rodata.str";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000103 PrivateGlobalPrefix = ".L";
104 WeakRefDirective = "\t.weak\t";
105 SetDirective = "\t.set\t";
106 DwarfRequiresFrameSection = false;
107 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",%progbits";
108 DwarfInfoSection = "\t.section\t.debug_info,\"\",%progbits";
109 DwarfLineSection = "\t.section\t.debug_line,\"\",%progbits";
110 DwarfFrameSection = "\t.section\t.debug_frame,\"\",%progbits";
111 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",%progbits";
112 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",%progbits";
113 DwarfStrSection = "\t.section\t.debug_str,\"\",%progbits";
114 DwarfLocSection = "\t.section\t.debug_loc,\"\",%progbits";
115 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",%progbits";
116 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",%progbits";
117 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",%progbits";
118
119 if (Subtarget->isAAPCS_ABI()) {
120 StaticCtorsSection = "\t.section .init_array,\"aw\",%init_array";
121 StaticDtorsSection = "\t.section .fini_array,\"aw\",%fini_array";
122 } else {
123 StaticCtorsSection = "\t.section .ctors,\"aw\",%progbits";
124 StaticDtorsSection = "\t.section .dtors,\"aw\",%progbits";
125 }
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000126}
127
Dale Johannesen67cf5612007-05-02 01:02:40 +0000128/// Count the number of comma-separated arguments.
129/// Do not try to detect errors.
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000130template <class BaseTAI>
131unsigned ARMTargetAsmInfo<BaseTAI>::countArguments(const char* p) const {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000132 unsigned count = 0;
133 while (*p && isspace(*p) && *p != '\n')
134 p++;
135 count++;
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000136 while (*p && *p!='\n' &&
137 strncmp(p, BaseTAI::CommentString,
138 strlen(BaseTAI::CommentString))!=0) {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000139 if (*p==',')
140 count++;
141 p++;
142 }
143 return count;
144}
145
146/// Count the length of a string enclosed in quote characters.
147/// Do not try to detect errors.
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000148template <class BaseTAI>
149unsigned ARMTargetAsmInfo<BaseTAI>::countString(const char* p) const {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000150 unsigned count = 0;
151 while (*p && isspace(*p) && *p!='\n')
152 p++;
153 if (!*p || *p != '\"')
154 return count;
155 while (*++p && *p != '\"')
156 count++;
157 return count;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000158}
Dale Johannesen86501992007-04-29 19:17:45 +0000159
160/// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000161template <class BaseTAI>
162unsigned ARMTargetAsmInfo<BaseTAI>::getInlineAsmLength(const char *s) const {
Dale Johannesenca4571e2007-10-25 21:54:43 +0000163 // Make a lowercase-folded version of s for counting purposes.
164 char *q, *s_copy = (char *)malloc(strlen(s) + 1);
165 strcpy(s_copy, s);
166 for (q=s_copy; *q; q++)
167 *q = tolower(*q);
168 const char *Str = s_copy;
169
Dale Johannesen86501992007-04-29 19:17:45 +0000170 // Count the number of bytes in the asm.
171 bool atInsnStart = true;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000172 bool inTextSection = true;
Dale Johannesen86501992007-04-29 19:17:45 +0000173 unsigned Length = 0;
174 for (; *Str; ++Str) {
175 if (atInsnStart) {
176 // Skip whitespace
177 while (*Str && isspace(*Str) && *Str != '\n')
178 Str++;
179 // Skip label
180 for (const char* p = Str; *p && !isspace(*p); p++)
181 if (*p == ':') {
182 Str = p+1;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000183 while (*Str && isspace(*Str) && *Str != '\n')
184 Str++;
Dale Johannesen86501992007-04-29 19:17:45 +0000185 break;
186 }
187 // Ignore everything from comment char(s) to EOL
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000188 if (strncmp(Str, BaseTAI::CommentString, strlen(BaseTAI::CommentString))==-0)
Dale Johannesen86501992007-04-29 19:17:45 +0000189 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000190 // FIXME do something like the following for non-Darwin
191 else if (*Str == '.' && Subtarget->isTargetDarwin()) {
192 // Directive.
193 atInsnStart = false;
Dale Johannesenca4571e2007-10-25 21:54:43 +0000194
Dale Johannesen67cf5612007-05-02 01:02:40 +0000195 // Some change the section, but don't generate code.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000196 if (strncmp(Str, ".literal4", strlen(".literal4"))==0 ||
197 strncmp(Str, ".literal8", strlen(".literal8"))==0 ||
198 strncmp(Str, ".const", strlen(".const"))==0 ||
199 strncmp(Str, ".constructor", strlen(".constructor"))==0 ||
200 strncmp(Str, ".cstring", strlen(".cstring"))==0 ||
201 strncmp(Str, ".data", strlen(".data"))==0 ||
202 strncmp(Str, ".destructor", strlen(".destructor"))==0 ||
203 strncmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
204 strncmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
205 strncmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
206 strncmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
207 strncmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
208 strncmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
209 strncmp(Str, ".static_data", strlen(".static_data"))==0 ||
210 strncmp(Str, ".section", strlen(".section"))==0 ||
211 strncmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
212 strncmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
213 strncmp(Str, ".dyld", strlen(".dyld"))==0 ||
214 strncmp(Str, ".const_data", strlen(".const_data"))==0 ||
215 strncmp(Str, ".objc", strlen(".objc"))==0 || //// many directives
216 strncmp(Str, ".static_const", strlen(".static_const"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000217 inTextSection=false;
Dale Johannesenca4571e2007-10-25 21:54:43 +0000218 else if (strncmp(Str, ".text", strlen(".text"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000219 inTextSection = true;
220 // Some can't really be handled without implementing significant pieces
221 // of an assembler. Others require dynamic adjustment of block sizes in
222 // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
223 // instruction in there, and none of these are currently used in the kernel.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000224 else if (strncmp(Str, ".macro", strlen(".macro"))==0 ||
225 strncmp(Str, ".if", strlen(".if"))==0 ||
226 strncmp(Str, ".align", strlen(".align"))==0 ||
227 strncmp(Str, ".fill", strlen(".fill"))==0 ||
228 strncmp(Str, ".space", strlen(".space"))==0 ||
229 strncmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
230 strncmp(Str, ".p2align", strlen(".p2align"))==0 ||
231 strncmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
232 strncmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
233 strncmp(Str, ".align32", strlen(".p2align32"))==0 ||
234 strncmp(Str, ".include", strlen(".include"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000235 cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
236 " constant pools (the assembler will tell you if this happens).\n";
237 // Some generate code, but this is only interesting in the text section.
238 else if (inTextSection) {
Dale Johannesenca4571e2007-10-25 21:54:43 +0000239 if (strncmp(Str, ".long", strlen(".long"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000240 Length += 4*countArguments(Str+strlen(".long"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000241 else if (strncmp(Str, ".short", strlen(".short"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000242 Length += 2*countArguments(Str+strlen(".short"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000243 else if (strncmp(Str, ".byte", strlen(".byte"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000244 Length += 1*countArguments(Str+strlen(".byte"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000245 else if (strncmp(Str, ".single", strlen(".single"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000246 Length += 4*countArguments(Str+strlen(".single"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000247 else if (strncmp(Str, ".double", strlen(".double"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000248 Length += 8*countArguments(Str+strlen(".double"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000249 else if (strncmp(Str, ".quad", strlen(".quad"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000250 Length += 16*countArguments(Str+strlen(".quad"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000251 else if (strncmp(Str, ".ascii", strlen(".ascii"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000252 Length += countString(Str+strlen(".ascii"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000253 else if (strncmp(Str, ".asciz", strlen(".asciz"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000254 Length += countString(Str+strlen(".asciz"))+1;
255 }
256 } else if (inTextSection) {
Dale Johannesen86501992007-04-29 19:17:45 +0000257 // An instruction
258 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000259 if (Subtarget->isThumb()) {
Dale Johannesen86501992007-04-29 19:17:45 +0000260 // BL and BLX <non-reg> are 4 bytes, all others 2.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000261 if (strncmp(Str, "blx", strlen("blx"))==0) {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000262 const char* p = Str+3;
263 while (*p && isspace(*p))
264 p++;
265 if (*p == 'r' || *p=='R')
266 Length += 2; // BLX reg
Dale Johannesen86501992007-04-29 19:17:45 +0000267 else
Dale Johannesen67cf5612007-05-02 01:02:40 +0000268 Length += 4; // BLX non-reg
Dale Johannesenca4571e2007-10-25 21:54:43 +0000269 } else if (strncmp(Str, "bl", strlen("bl"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000270 Length += 4; // BL
271 else
Dale Johannesen86501992007-04-29 19:17:45 +0000272 Length += 2; // Thumb anything else
273 }
274 else
275 Length += 4; // ARM
276 }
277 }
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000278 if (*Str == '\n' || *Str == BaseTAI::SeparatorChar)
Dale Johannesen86501992007-04-29 19:17:45 +0000279 atInsnStart = true;
280 }
Dale Johannesenca4571e2007-10-25 21:54:43 +0000281 free(s_copy);
Dale Johannesen86501992007-04-29 19:17:45 +0000282 return Length;
283}
Anton Korobeynikov6381a132008-10-05 08:53:29 +0000284
285// Instantiate default implementation.
286TEMPLATE_INSTANTIATION(class ARMTargetAsmInfo<TargetAsmInfo>);