blob: aae3712fb130e7ac4f142ca2f8f53c13ff2cd5a7 [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;
64 NeedsIndirectEncoding = true;
65 if (TM.getRelocationModel() == Reloc::Static) {
66 StaticCtorsSection = ".constructor";
67 StaticDtorsSection = ".destructor";
68 } else {
69 StaticCtorsSection = ".mod_init_func";
70 StaticDtorsSection = ".mod_term_func";
71 }
72
73 // In non-PIC modes, emit a special label before jump tables so that the
74 // linker can perform more accurate dead code stripping.
75 if (TM.getRelocationModel() != Reloc::PIC_) {
76 // Emit a local label that is preserved until the linker runs.
77 JumpTableSpecialLabelPrefix = "l";
78 }
79
80 NeedsSet = true;
81 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
82 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
83 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
84 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
85 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
86 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
87 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
88 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
89 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
90 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
91 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
92}
93
94ARMELFTargetAsmInfo::ARMELFTargetAsmInfo(const ARMTargetMachine &TM):
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000095 ARMTargetAsmInfo<ELFTargetAsmInfo>(TM) {
Dan Gohman8f092252008-11-03 18:22:42 +000096 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000097
98 NeedsSet = false;
99 HasLEB128 = true;
100 AbsoluteDebugSectionOffsets = true;
101 CStringSection = ".rodata.str";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000102 PrivateGlobalPrefix = ".L";
103 WeakRefDirective = "\t.weak\t";
104 SetDirective = "\t.set\t";
105 DwarfRequiresFrameSection = false;
106 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",%progbits";
107 DwarfInfoSection = "\t.section\t.debug_info,\"\",%progbits";
108 DwarfLineSection = "\t.section\t.debug_line,\"\",%progbits";
109 DwarfFrameSection = "\t.section\t.debug_frame,\"\",%progbits";
110 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",%progbits";
111 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",%progbits";
112 DwarfStrSection = "\t.section\t.debug_str,\"\",%progbits";
113 DwarfLocSection = "\t.section\t.debug_loc,\"\",%progbits";
114 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",%progbits";
115 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",%progbits";
116 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",%progbits";
117
118 if (Subtarget->isAAPCS_ABI()) {
119 StaticCtorsSection = "\t.section .init_array,\"aw\",%init_array";
120 StaticDtorsSection = "\t.section .fini_array,\"aw\",%fini_array";
121 } else {
122 StaticCtorsSection = "\t.section .ctors,\"aw\",%progbits";
123 StaticDtorsSection = "\t.section .dtors,\"aw\",%progbits";
124 }
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000125}
126
Dale Johannesen67cf5612007-05-02 01:02:40 +0000127/// Count the number of comma-separated arguments.
128/// Do not try to detect errors.
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000129template <class BaseTAI>
130unsigned ARMTargetAsmInfo<BaseTAI>::countArguments(const char* p) const {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000131 unsigned count = 0;
132 while (*p && isspace(*p) && *p != '\n')
133 p++;
134 count++;
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000135 while (*p && *p!='\n' &&
136 strncmp(p, BaseTAI::CommentString,
137 strlen(BaseTAI::CommentString))!=0) {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000138 if (*p==',')
139 count++;
140 p++;
141 }
142 return count;
143}
144
145/// Count the length of a string enclosed in quote characters.
146/// Do not try to detect errors.
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000147template <class BaseTAI>
148unsigned ARMTargetAsmInfo<BaseTAI>::countString(const char* p) const {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000149 unsigned count = 0;
150 while (*p && isspace(*p) && *p!='\n')
151 p++;
152 if (!*p || *p != '\"')
153 return count;
154 while (*++p && *p != '\"')
155 count++;
156 return count;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000157}
Dale Johannesen86501992007-04-29 19:17:45 +0000158
159/// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000160template <class BaseTAI>
161unsigned ARMTargetAsmInfo<BaseTAI>::getInlineAsmLength(const char *s) const {
Dale Johannesenca4571e2007-10-25 21:54:43 +0000162 // Make a lowercase-folded version of s for counting purposes.
163 char *q, *s_copy = (char *)malloc(strlen(s) + 1);
164 strcpy(s_copy, s);
165 for (q=s_copy; *q; q++)
166 *q = tolower(*q);
167 const char *Str = s_copy;
168
Dale Johannesen86501992007-04-29 19:17:45 +0000169 // Count the number of bytes in the asm.
170 bool atInsnStart = true;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000171 bool inTextSection = true;
Dale Johannesen86501992007-04-29 19:17:45 +0000172 unsigned Length = 0;
173 for (; *Str; ++Str) {
174 if (atInsnStart) {
175 // Skip whitespace
176 while (*Str && isspace(*Str) && *Str != '\n')
177 Str++;
178 // Skip label
179 for (const char* p = Str; *p && !isspace(*p); p++)
180 if (*p == ':') {
181 Str = p+1;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000182 while (*Str && isspace(*Str) && *Str != '\n')
183 Str++;
Dale Johannesen86501992007-04-29 19:17:45 +0000184 break;
185 }
186 // Ignore everything from comment char(s) to EOL
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000187 if (strncmp(Str, BaseTAI::CommentString, strlen(BaseTAI::CommentString))==-0)
Dale Johannesen86501992007-04-29 19:17:45 +0000188 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000189 // FIXME do something like the following for non-Darwin
190 else if (*Str == '.' && Subtarget->isTargetDarwin()) {
191 // Directive.
192 atInsnStart = false;
Dale Johannesenca4571e2007-10-25 21:54:43 +0000193
Dale Johannesen67cf5612007-05-02 01:02:40 +0000194 // Some change the section, but don't generate code.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000195 if (strncmp(Str, ".literal4", strlen(".literal4"))==0 ||
196 strncmp(Str, ".literal8", strlen(".literal8"))==0 ||
197 strncmp(Str, ".const", strlen(".const"))==0 ||
198 strncmp(Str, ".constructor", strlen(".constructor"))==0 ||
199 strncmp(Str, ".cstring", strlen(".cstring"))==0 ||
200 strncmp(Str, ".data", strlen(".data"))==0 ||
201 strncmp(Str, ".destructor", strlen(".destructor"))==0 ||
202 strncmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
203 strncmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
204 strncmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
205 strncmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
206 strncmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
207 strncmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
208 strncmp(Str, ".static_data", strlen(".static_data"))==0 ||
209 strncmp(Str, ".section", strlen(".section"))==0 ||
210 strncmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
211 strncmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
212 strncmp(Str, ".dyld", strlen(".dyld"))==0 ||
213 strncmp(Str, ".const_data", strlen(".const_data"))==0 ||
214 strncmp(Str, ".objc", strlen(".objc"))==0 || //// many directives
215 strncmp(Str, ".static_const", strlen(".static_const"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000216 inTextSection=false;
Dale Johannesenca4571e2007-10-25 21:54:43 +0000217 else if (strncmp(Str, ".text", strlen(".text"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000218 inTextSection = true;
219 // Some can't really be handled without implementing significant pieces
220 // of an assembler. Others require dynamic adjustment of block sizes in
221 // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
222 // instruction in there, and none of these are currently used in the kernel.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000223 else if (strncmp(Str, ".macro", strlen(".macro"))==0 ||
224 strncmp(Str, ".if", strlen(".if"))==0 ||
225 strncmp(Str, ".align", strlen(".align"))==0 ||
226 strncmp(Str, ".fill", strlen(".fill"))==0 ||
227 strncmp(Str, ".space", strlen(".space"))==0 ||
228 strncmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
229 strncmp(Str, ".p2align", strlen(".p2align"))==0 ||
230 strncmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
231 strncmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
232 strncmp(Str, ".align32", strlen(".p2align32"))==0 ||
233 strncmp(Str, ".include", strlen(".include"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000234 cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
235 " constant pools (the assembler will tell you if this happens).\n";
236 // Some generate code, but this is only interesting in the text section.
237 else if (inTextSection) {
Dale Johannesenca4571e2007-10-25 21:54:43 +0000238 if (strncmp(Str, ".long", strlen(".long"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000239 Length += 4*countArguments(Str+strlen(".long"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000240 else if (strncmp(Str, ".short", strlen(".short"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000241 Length += 2*countArguments(Str+strlen(".short"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000242 else if (strncmp(Str, ".byte", strlen(".byte"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000243 Length += 1*countArguments(Str+strlen(".byte"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000244 else if (strncmp(Str, ".single", strlen(".single"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000245 Length += 4*countArguments(Str+strlen(".single"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000246 else if (strncmp(Str, ".double", strlen(".double"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000247 Length += 8*countArguments(Str+strlen(".double"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000248 else if (strncmp(Str, ".quad", strlen(".quad"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000249 Length += 16*countArguments(Str+strlen(".quad"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000250 else if (strncmp(Str, ".ascii", strlen(".ascii"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000251 Length += countString(Str+strlen(".ascii"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000252 else if (strncmp(Str, ".asciz", strlen(".asciz"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000253 Length += countString(Str+strlen(".asciz"))+1;
254 }
255 } else if (inTextSection) {
Dale Johannesen86501992007-04-29 19:17:45 +0000256 // An instruction
257 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000258 if (Subtarget->isThumb()) {
Dale Johannesen86501992007-04-29 19:17:45 +0000259 // BL and BLX <non-reg> are 4 bytes, all others 2.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000260 if (strncmp(Str, "blx", strlen("blx"))==0) {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000261 const char* p = Str+3;
262 while (*p && isspace(*p))
263 p++;
264 if (*p == 'r' || *p=='R')
265 Length += 2; // BLX reg
Dale Johannesen86501992007-04-29 19:17:45 +0000266 else
Dale Johannesen67cf5612007-05-02 01:02:40 +0000267 Length += 4; // BLX non-reg
Dale Johannesenca4571e2007-10-25 21:54:43 +0000268 } else if (strncmp(Str, "bl", strlen("bl"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000269 Length += 4; // BL
270 else
Dale Johannesen86501992007-04-29 19:17:45 +0000271 Length += 2; // Thumb anything else
272 }
273 else
274 Length += 4; // ARM
275 }
276 }
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000277 if (*Str == '\n' || *Str == BaseTAI::SeparatorChar)
Dale Johannesen86501992007-04-29 19:17:45 +0000278 atInsnStart = true;
279 }
Dale Johannesenca4571e2007-10-25 21:54:43 +0000280 free(s_copy);
Dale Johannesen86501992007-04-29 19:17:45 +0000281 return Length;
282}
Anton Korobeynikov6381a132008-10-05 08:53:29 +0000283
284// Instantiate default implementation.
285TEMPLATE_INSTANTIATION(class ARMTargetAsmInfo<TargetAsmInfo>);