blob: 7afbab344b822ec02fe2e3969c844244ef2029ec [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
Evan Cheng688535e2008-09-25 07:38:08 +000020static const char *const arm_asm_table[] = {
Dan Gohmancfbb2f02008-03-25 21:45:14 +000021 "{r0}", "r0",
Lauro Ramos Venancio61718a62007-06-08 21:06:23 +000022 "{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
Evan Cheng688535e2008-09-25 07:38:08 +000045ARMTargetAsmInfo::ARMTargetAsmInfo(const ARMTargetMachine &TM) {
46 AsmTransCBE = arm_asm_table;
47
48 AlignmentIsInBytes = false;
49 Data64bitsDirective = 0;
50 CommentString = "@";
51 ConstantPoolSection = "\t.text\n";
52 COMMDirectiveTakesAlignment = false;
53 InlineAsmStart = "@ InlineAsm Start";
54 InlineAsmEnd = "@ InlineAsm End";
55 LCOMMDirective = "\t.lcomm\t";
56}
Dale Johannesen67cf5612007-05-02 01:02:40 +000057
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000058ARMDarwinTargetAsmInfo::ARMDarwinTargetAsmInfo(const ARMTargetMachine &TM):
Evan Cheng688535e2008-09-25 07:38:08 +000059 ARMTargetAsmInfo(TM), DarwinTargetAsmInfo(TM) {
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000060 Subtarget = &DTM->getSubtarget<ARMSubtarget>();
61
62 GlobalPrefix = "_";
63 PrivateGlobalPrefix = "L";
Dale Johannesenb2dfb892008-09-09 01:21:22 +000064 LessPrivateGlobalPrefix = "l";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000065 StringConstantPrefix = "\1LC";
66 BSSSection = 0; // no BSS section
67 ZeroDirective = "\t.space\t";
68 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
69 SetDirective = "\t.set\t";
70 WeakRefDirective = "\t.weak_reference\t";
71 HiddenDirective = "\t.private_extern\t";
72 ProtectedDirective = NULL;
73 JumpTableDataSection = ".const";
74 CStringSection = "\t.cstring";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000075 HasDotTypeDotSizeDirective = false;
76 NeedsIndirectEncoding = true;
77 if (TM.getRelocationModel() == Reloc::Static) {
78 StaticCtorsSection = ".constructor";
79 StaticDtorsSection = ".destructor";
80 } else {
81 StaticCtorsSection = ".mod_init_func";
82 StaticDtorsSection = ".mod_term_func";
83 }
84
85 // In non-PIC modes, emit a special label before jump tables so that the
86 // linker can perform more accurate dead code stripping.
87 if (TM.getRelocationModel() != Reloc::PIC_) {
88 // Emit a local label that is preserved until the linker runs.
89 JumpTableSpecialLabelPrefix = "l";
90 }
91
92 NeedsSet = true;
93 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
94 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
95 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
96 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
97 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
98 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
99 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
100 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
101 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
102 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
103 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
104}
105
106ARMELFTargetAsmInfo::ARMELFTargetAsmInfo(const ARMTargetMachine &TM):
Evan Cheng688535e2008-09-25 07:38:08 +0000107 ARMTargetAsmInfo(TM), ELFTargetAsmInfo(TM) {
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000108 Subtarget = &ETM->getSubtarget<ARMSubtarget>();
109
110 NeedsSet = false;
111 HasLEB128 = true;
112 AbsoluteDebugSectionOffsets = true;
113 CStringSection = ".rodata.str";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000114 PrivateGlobalPrefix = ".L";
115 WeakRefDirective = "\t.weak\t";
116 SetDirective = "\t.set\t";
117 DwarfRequiresFrameSection = false;
118 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",%progbits";
119 DwarfInfoSection = "\t.section\t.debug_info,\"\",%progbits";
120 DwarfLineSection = "\t.section\t.debug_line,\"\",%progbits";
121 DwarfFrameSection = "\t.section\t.debug_frame,\"\",%progbits";
122 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",%progbits";
123 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",%progbits";
124 DwarfStrSection = "\t.section\t.debug_str,\"\",%progbits";
125 DwarfLocSection = "\t.section\t.debug_loc,\"\",%progbits";
126 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",%progbits";
127 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",%progbits";
128 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",%progbits";
129
130 if (Subtarget->isAAPCS_ABI()) {
131 StaticCtorsSection = "\t.section .init_array,\"aw\",%init_array";
132 StaticDtorsSection = "\t.section .fini_array,\"aw\",%fini_array";
133 } else {
134 StaticCtorsSection = "\t.section .ctors,\"aw\",%progbits";
135 StaticDtorsSection = "\t.section .dtors,\"aw\",%progbits";
136 }
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000137}
138
Dale Johannesen67cf5612007-05-02 01:02:40 +0000139/// Count the number of comma-separated arguments.
140/// Do not try to detect errors.
Evan Cheng688535e2008-09-25 07:38:08 +0000141unsigned ARMTargetAsmInfo::countArguments(const char* p) const {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000142 unsigned count = 0;
143 while (*p && isspace(*p) && *p != '\n')
144 p++;
145 count++;
Evan Cheng688535e2008-09-25 07:38:08 +0000146 while (*p && *p!='\n' &&
147 strncmp(p, CommentString, strlen(CommentString))!=0) {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000148 if (*p==',')
149 count++;
150 p++;
151 }
152 return count;
153}
154
155/// Count the length of a string enclosed in quote characters.
156/// Do not try to detect errors.
Evan Cheng688535e2008-09-25 07:38:08 +0000157unsigned ARMTargetAsmInfo::countString(const char* p) const {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000158 unsigned count = 0;
159 while (*p && isspace(*p) && *p!='\n')
160 p++;
161 if (!*p || *p != '\"')
162 return count;
163 while (*++p && *p != '\"')
164 count++;
165 return count;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000166}
Dale Johannesen86501992007-04-29 19:17:45 +0000167
168/// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
Evan Cheng688535e2008-09-25 07:38:08 +0000169unsigned ARMTargetAsmInfo::getInlineAsmLength(const char *s) const {
Dale Johannesenca4571e2007-10-25 21:54:43 +0000170 // Make a lowercase-folded version of s for counting purposes.
171 char *q, *s_copy = (char *)malloc(strlen(s) + 1);
172 strcpy(s_copy, s);
173 for (q=s_copy; *q; q++)
174 *q = tolower(*q);
175 const char *Str = s_copy;
176
Dale Johannesen86501992007-04-29 19:17:45 +0000177 // Count the number of bytes in the asm.
178 bool atInsnStart = true;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000179 bool inTextSection = true;
Dale Johannesen86501992007-04-29 19:17:45 +0000180 unsigned Length = 0;
181 for (; *Str; ++Str) {
182 if (atInsnStart) {
183 // Skip whitespace
184 while (*Str && isspace(*Str) && *Str != '\n')
185 Str++;
186 // Skip label
187 for (const char* p = Str; *p && !isspace(*p); p++)
188 if (*p == ':') {
189 Str = p+1;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000190 while (*Str && isspace(*Str) && *Str != '\n')
191 Str++;
Dale Johannesen86501992007-04-29 19:17:45 +0000192 break;
193 }
194 // Ignore everything from comment char(s) to EOL
Evan Cheng688535e2008-09-25 07:38:08 +0000195 if (strncmp(Str, CommentString, strlen(CommentString))==-0)
Dale Johannesen86501992007-04-29 19:17:45 +0000196 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000197 // FIXME do something like the following for non-Darwin
198 else if (*Str == '.' && Subtarget->isTargetDarwin()) {
199 // Directive.
200 atInsnStart = false;
Dale Johannesenca4571e2007-10-25 21:54:43 +0000201
Dale Johannesen67cf5612007-05-02 01:02:40 +0000202 // Some change the section, but don't generate code.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000203 if (strncmp(Str, ".literal4", strlen(".literal4"))==0 ||
204 strncmp(Str, ".literal8", strlen(".literal8"))==0 ||
205 strncmp(Str, ".const", strlen(".const"))==0 ||
206 strncmp(Str, ".constructor", strlen(".constructor"))==0 ||
207 strncmp(Str, ".cstring", strlen(".cstring"))==0 ||
208 strncmp(Str, ".data", strlen(".data"))==0 ||
209 strncmp(Str, ".destructor", strlen(".destructor"))==0 ||
210 strncmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
211 strncmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
212 strncmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
213 strncmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
214 strncmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
215 strncmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
216 strncmp(Str, ".static_data", strlen(".static_data"))==0 ||
217 strncmp(Str, ".section", strlen(".section"))==0 ||
218 strncmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
219 strncmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
220 strncmp(Str, ".dyld", strlen(".dyld"))==0 ||
221 strncmp(Str, ".const_data", strlen(".const_data"))==0 ||
222 strncmp(Str, ".objc", strlen(".objc"))==0 || //// many directives
223 strncmp(Str, ".static_const", strlen(".static_const"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000224 inTextSection=false;
Dale Johannesenca4571e2007-10-25 21:54:43 +0000225 else if (strncmp(Str, ".text", strlen(".text"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000226 inTextSection = true;
227 // Some can't really be handled without implementing significant pieces
228 // of an assembler. Others require dynamic adjustment of block sizes in
229 // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
230 // instruction in there, and none of these are currently used in the kernel.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000231 else if (strncmp(Str, ".macro", strlen(".macro"))==0 ||
232 strncmp(Str, ".if", strlen(".if"))==0 ||
233 strncmp(Str, ".align", strlen(".align"))==0 ||
234 strncmp(Str, ".fill", strlen(".fill"))==0 ||
235 strncmp(Str, ".space", strlen(".space"))==0 ||
236 strncmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
237 strncmp(Str, ".p2align", strlen(".p2align"))==0 ||
238 strncmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
239 strncmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
240 strncmp(Str, ".align32", strlen(".p2align32"))==0 ||
241 strncmp(Str, ".include", strlen(".include"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000242 cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
243 " constant pools (the assembler will tell you if this happens).\n";
244 // Some generate code, but this is only interesting in the text section.
245 else if (inTextSection) {
Dale Johannesenca4571e2007-10-25 21:54:43 +0000246 if (strncmp(Str, ".long", strlen(".long"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000247 Length += 4*countArguments(Str+strlen(".long"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000248 else if (strncmp(Str, ".short", strlen(".short"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000249 Length += 2*countArguments(Str+strlen(".short"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000250 else if (strncmp(Str, ".byte", strlen(".byte"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000251 Length += 1*countArguments(Str+strlen(".byte"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000252 else if (strncmp(Str, ".single", strlen(".single"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000253 Length += 4*countArguments(Str+strlen(".single"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000254 else if (strncmp(Str, ".double", strlen(".double"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000255 Length += 8*countArguments(Str+strlen(".double"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000256 else if (strncmp(Str, ".quad", strlen(".quad"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000257 Length += 16*countArguments(Str+strlen(".quad"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000258 else if (strncmp(Str, ".ascii", strlen(".ascii"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000259 Length += countString(Str+strlen(".ascii"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000260 else if (strncmp(Str, ".asciz", strlen(".asciz"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000261 Length += countString(Str+strlen(".asciz"))+1;
262 }
263 } else if (inTextSection) {
Dale Johannesen86501992007-04-29 19:17:45 +0000264 // An instruction
265 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000266 if (Subtarget->isThumb()) {
Dale Johannesen86501992007-04-29 19:17:45 +0000267 // BL and BLX <non-reg> are 4 bytes, all others 2.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000268 if (strncmp(Str, "blx", strlen("blx"))==0) {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000269 const char* p = Str+3;
270 while (*p && isspace(*p))
271 p++;
272 if (*p == 'r' || *p=='R')
273 Length += 2; // BLX reg
Dale Johannesen86501992007-04-29 19:17:45 +0000274 else
Dale Johannesen67cf5612007-05-02 01:02:40 +0000275 Length += 4; // BLX non-reg
Dale Johannesenca4571e2007-10-25 21:54:43 +0000276 } else if (strncmp(Str, "bl", strlen("bl"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000277 Length += 4; // BL
278 else
Dale Johannesen86501992007-04-29 19:17:45 +0000279 Length += 2; // Thumb anything else
280 }
281 else
282 Length += 4; // ARM
283 }
284 }
Evan Cheng688535e2008-09-25 07:38:08 +0000285 if (*Str == '\n' || *Str == SeparatorChar)
Dale Johannesen86501992007-04-29 19:17:45 +0000286 atInsnStart = true;
287 }
Dale Johannesenca4571e2007-10-25 21:54:43 +0000288 free(s_copy);
Dale Johannesen86501992007-04-29 19:17:45 +0000289 return Length;
290}