blob: 47abcc6f40776bd9d9501591034c8b3e2353ec72 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ARMTargetAsmInfo.cpp - ARM asm properties ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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>
18using namespace llvm;
19
Dan Gohman12300e12008-03-25 21:45:14 +000020static const char *const arm_asm_table[] = {
21 "{r0}", "r0",
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
45ARMTargetAsmInfo::ARMTargetAsmInfo(const ARMTargetMachine &TM) {
46 Subtarget = &TM.getSubtarget<ARMSubtarget>();
47 AsmTransCBE = arm_asm_table;
48 if (Subtarget->isTargetDarwin()) {
49 GlobalPrefix = "_";
50 PrivateGlobalPrefix = "L";
Dale Johannesen8f03a202008-06-03 18:09:06 +000051 StringConstantPrefix = "\1LC";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 BSSSection = 0; // no BSS section.
53 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
Evan Chengf0688652008-03-24 23:36:49 +000054 SetDirective = "\t.set\t";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 WeakRefDirective = "\t.weak_reference\t";
56 HiddenDirective = "\t.private_extern\t";
57 ProtectedDirective = NULL;
58 JumpTableDataSection = ".const";
59 CStringSection = "\t.cstring";
60 FourByteConstantSection = "\t.literal4\n";
61 EightByteConstantSection = "\t.literal8\n";
62 ReadOnlySection = "\t.const\n";
63 HasDotTypeDotSizeDirective = false;
Bill Wendling909ec562007-09-11 23:55:40 +000064 NeedsIndirectEncoding = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 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 } else {
93 NeedsSet = false;
94 HasLEB128 = true;
95 AbsoluteDebugSectionOffsets = true;
96 ReadOnlySection = "\t.section\t.rodata\n";
97 PrivateGlobalPrefix = ".L";
98 WeakRefDirective = "\t.weak\t";
99 SetDirective = "\t.set\t";
100 DwarfRequiresFrameSection = false;
101 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",%progbits";
102 DwarfInfoSection = "\t.section\t.debug_info,\"\",%progbits";
103 DwarfLineSection = "\t.section\t.debug_line,\"\",%progbits";
104 DwarfFrameSection = "\t.section\t.debug_frame,\"\",%progbits";
105 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",%progbits";
106 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",%progbits";
107 DwarfStrSection = "\t.section\t.debug_str,\"\",%progbits";
108 DwarfLocSection = "\t.section\t.debug_loc,\"\",%progbits";
109 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",%progbits";
110 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",%progbits";
111 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",%progbits";
112
113 if (Subtarget->isAAPCS_ABI()) {
114 StaticCtorsSection = "\t.section .init_array,\"aw\",%init_array";
115 StaticDtorsSection = "\t.section .fini_array,\"aw\",%fini_array";
116 } else {
117 StaticCtorsSection = "\t.section .ctors,\"aw\",%progbits";
118 StaticDtorsSection = "\t.section .dtors,\"aw\",%progbits";
119 }
120 TLSDataSection = "\t.section .tdata,\"awT\",%progbits";
121 TLSBSSSection = "\t.section .tbss,\"awT\",%nobits";
122 }
123
124 ZeroDirective = "\t.space\t";
125 AlignmentIsInBytes = false;
126 Data64bitsDirective = 0;
127 CommentString = "@";
128 DataSection = "\t.data";
129 ConstantPoolSection = "\t.text\n";
130 COMMDirectiveTakesAlignment = false;
131 InlineAsmStart = "@ InlineAsm Start";
132 InlineAsmEnd = "@ InlineAsm End";
133 LCOMMDirective = "\t.lcomm\t";
134}
135
136/// Count the number of comma-separated arguments.
137/// Do not try to detect errors.
138unsigned ARMTargetAsmInfo::countArguments(const char* p) const {
139 unsigned count = 0;
140 while (*p && isspace(*p) && *p != '\n')
141 p++;
142 count++;
143 while (*p && *p!='\n' &&
144 strncmp(p, CommentString, strlen(CommentString))!=0) {
145 if (*p==',')
146 count++;
147 p++;
148 }
149 return count;
150}
151
152/// Count the length of a string enclosed in quote characters.
153/// Do not try to detect errors.
154unsigned ARMTargetAsmInfo::countString(const char* p) const {
155 unsigned count = 0;
156 while (*p && isspace(*p) && *p!='\n')
157 p++;
158 if (!*p || *p != '\"')
159 return count;
160 while (*++p && *p != '\"')
161 count++;
162 return count;
163}
164
165/// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000166unsigned ARMTargetAsmInfo::getInlineAsmLength(const char *s) const {
167 // Make a lowercase-folded version of s for counting purposes.
168 char *q, *s_copy = (char *)malloc(strlen(s) + 1);
169 strcpy(s_copy, s);
170 for (q=s_copy; *q; q++)
171 *q = tolower(*q);
172 const char *Str = s_copy;
173
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 // Count the number of bytes in the asm.
175 bool atInsnStart = true;
176 bool inTextSection = true;
177 unsigned Length = 0;
178 for (; *Str; ++Str) {
179 if (atInsnStart) {
180 // Skip whitespace
181 while (*Str && isspace(*Str) && *Str != '\n')
182 Str++;
183 // Skip label
184 for (const char* p = Str; *p && !isspace(*p); p++)
185 if (*p == ':') {
186 Str = p+1;
187 while (*Str && isspace(*Str) && *Str != '\n')
188 Str++;
189 break;
190 }
191 // Ignore everything from comment char(s) to EOL
192 if (strncmp(Str, CommentString, strlen(CommentString))==-0)
193 atInsnStart = false;
194 // FIXME do something like the following for non-Darwin
195 else if (*Str == '.' && Subtarget->isTargetDarwin()) {
196 // Directive.
197 atInsnStart = false;
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000198
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 // Some change the section, but don't generate code.
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000200 if (strncmp(Str, ".literal4", strlen(".literal4"))==0 ||
201 strncmp(Str, ".literal8", strlen(".literal8"))==0 ||
202 strncmp(Str, ".const", strlen(".const"))==0 ||
203 strncmp(Str, ".constructor", strlen(".constructor"))==0 ||
204 strncmp(Str, ".cstring", strlen(".cstring"))==0 ||
205 strncmp(Str, ".data", strlen(".data"))==0 ||
206 strncmp(Str, ".destructor", strlen(".destructor"))==0 ||
207 strncmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
208 strncmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
209 strncmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
210 strncmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
211 strncmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
212 strncmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
213 strncmp(Str, ".static_data", strlen(".static_data"))==0 ||
214 strncmp(Str, ".section", strlen(".section"))==0 ||
215 strncmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
216 strncmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
217 strncmp(Str, ".dyld", strlen(".dyld"))==0 ||
218 strncmp(Str, ".const_data", strlen(".const_data"))==0 ||
219 strncmp(Str, ".objc", strlen(".objc"))==0 || //// many directives
220 strncmp(Str, ".static_const", strlen(".static_const"))==0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 inTextSection=false;
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000222 else if (strncmp(Str, ".text", strlen(".text"))==0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 inTextSection = true;
224 // Some can't really be handled without implementing significant pieces
225 // of an assembler. Others require dynamic adjustment of block sizes in
226 // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
227 // instruction in there, and none of these are currently used in the kernel.
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000228 else if (strncmp(Str, ".macro", strlen(".macro"))==0 ||
229 strncmp(Str, ".if", strlen(".if"))==0 ||
230 strncmp(Str, ".align", strlen(".align"))==0 ||
231 strncmp(Str, ".fill", strlen(".fill"))==0 ||
232 strncmp(Str, ".space", strlen(".space"))==0 ||
233 strncmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
234 strncmp(Str, ".p2align", strlen(".p2align"))==0 ||
235 strncmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
236 strncmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
237 strncmp(Str, ".align32", strlen(".p2align32"))==0 ||
238 strncmp(Str, ".include", strlen(".include"))==0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
240 " constant pools (the assembler will tell you if this happens).\n";
241 // Some generate code, but this is only interesting in the text section.
242 else if (inTextSection) {
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000243 if (strncmp(Str, ".long", strlen(".long"))==0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 Length += 4*countArguments(Str+strlen(".long"));
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000245 else if (strncmp(Str, ".short", strlen(".short"))==0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 Length += 2*countArguments(Str+strlen(".short"));
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000247 else if (strncmp(Str, ".byte", strlen(".byte"))==0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 Length += 1*countArguments(Str+strlen(".byte"));
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000249 else if (strncmp(Str, ".single", strlen(".single"))==0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 Length += 4*countArguments(Str+strlen(".single"));
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000251 else if (strncmp(Str, ".double", strlen(".double"))==0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 Length += 8*countArguments(Str+strlen(".double"));
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000253 else if (strncmp(Str, ".quad", strlen(".quad"))==0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 Length += 16*countArguments(Str+strlen(".quad"));
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000255 else if (strncmp(Str, ".ascii", strlen(".ascii"))==0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 Length += countString(Str+strlen(".ascii"));
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000257 else if (strncmp(Str, ".asciz", strlen(".asciz"))==0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 Length += countString(Str+strlen(".asciz"))+1;
259 }
260 } else if (inTextSection) {
261 // An instruction
262 atInsnStart = false;
263 if (Subtarget->isThumb()) {
264 // BL and BLX <non-reg> are 4 bytes, all others 2.
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000265 if (strncmp(Str, "blx", strlen("blx"))==0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 const char* p = Str+3;
267 while (*p && isspace(*p))
268 p++;
269 if (*p == 'r' || *p=='R')
270 Length += 2; // BLX reg
271 else
272 Length += 4; // BLX non-reg
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000273 } else if (strncmp(Str, "bl", strlen("bl"))==0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 Length += 4; // BL
275 else
276 Length += 2; // Thumb anything else
277 }
278 else
279 Length += 4; // ARM
280 }
281 }
282 if (*Str == '\n' || *Str == SeparatorChar)
283 atInsnStart = true;
284 }
Dale Johannesen3fdb2022007-10-25 21:54:43 +0000285 free(s_copy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 return Length;
287}