blob: 55ab20d47493c1c69ff0b1568ca8801d9cd0dc25 [file] [log] [blame]
Dale Johannesen67cf5612007-05-02 01:02:40 +00001
Jim Laskey8e8de8f2006-09-07 22:05:02 +00002//===-- ARMTargetAsmInfo.cpp - ARM asm properties ---------------*- C++ -*-===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file was developed by James M. Laskey and is distributed under the
7// University of Illinois Open Source License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10//
11// This file contains the declarations of the ARMTargetAsmInfo properties.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ARMTargetAsmInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARMTargetMachine.h"
Dale Johannesen24fb52d2007-04-23 20:04:35 +000017#include <cstring>
18#include <cctype>
Jim Laskey8e8de8f2006-09-07 22:05:02 +000019using namespace llvm;
20
Lauro Ramos Venancio61718a62007-06-08 21:06:23 +000021static const char* arm_asm_table[] = {"{r0}", "r0",
22 "{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
Jim Laskey8e8de8f2006-09-07 22:05:02 +000045ARMTargetAsmInfo::ARMTargetAsmInfo(const ARMTargetMachine &TM) {
Dale Johannesen67cf5612007-05-02 01:02:40 +000046 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Lauro Ramos Venancio61718a62007-06-08 21:06:23 +000047 AsmTransCBE = arm_asm_table;
Evan Chenge433ea92007-01-19 19:23:47 +000048 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +000049 GlobalPrefix = "_";
Evan Chenge433ea92007-01-19 19:23:47 +000050 PrivateGlobalPrefix = "L";
51 BSSSection = 0; // no BSS section.
52 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
Evan Chenga8e29892007-01-19 07:51:42 +000053 SetDirective = "\t.set";
54 WeakRefDirective = "\t.weak_reference\t";
Evan Chenga6f567c2007-01-23 19:06:03 +000055 HiddenDirective = "\t.private_extern\t";
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +000056 ProtectedDirective = NULL;
Evan Chenga8e29892007-01-19 07:51:42 +000057 JumpTableDataSection = ".const";
58 CStringSection = "\t.cstring";
Evan Cheng98ded762007-03-08 01:25:25 +000059 FourByteConstantSection = "\t.literal4\n";
60 EightByteConstantSection = "\t.literal8\n";
61 ReadOnlySection = "\t.const\n";
Evan Chenge433ea92007-01-19 19:23:47 +000062 HasDotTypeDotSizeDirective = false;
Bill Wendlingfe4afb12007-09-11 23:55:40 +000063 NeedsIndirectEncoding = true;
Evan Chengb267ca12007-01-30 08:04:53 +000064 if (TM.getRelocationModel() == Reloc::Static) {
65 StaticCtorsSection = ".constructor";
66 StaticDtorsSection = ".destructor";
67 } else {
68 StaticCtorsSection = ".mod_init_func";
69 StaticDtorsSection = ".mod_term_func";
70 }
Evan Chenga8e29892007-01-19 07:51:42 +000071
Evan Cheng8e1185b2007-01-19 18:59:56 +000072 // In non-PIC modes, emit a special label before jump tables so that the
73 // linker can perform more accurate dead code stripping.
74 if (TM.getRelocationModel() != Reloc::PIC_) {
75 // Emit a local label that is preserved until the linker runs.
76 JumpTableSpecialLabelPrefix = "l";
77 }
78
Evan Chenga8e29892007-01-19 07:51:42 +000079 NeedsSet = true;
80 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
81 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
82 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
83 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
84 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
85 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
86 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
87 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
88 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
89 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
90 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
91 } else {
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +000092 NeedsSet = false;
93 HasLEB128 = true;
94 AbsoluteDebugSectionOffsets = true;
95 ReadOnlySection = "\t.section\t.rodata\n";
Lauro Ramos Venancioc33f6742007-02-01 21:43:53 +000096 PrivateGlobalPrefix = ".L";
Evan Chenga8e29892007-01-19 07:51:42 +000097 WeakRefDirective = "\t.weak\t";
Lauro Ramos Venancio69642f12007-05-14 18:32:56 +000098 SetDirective = "\t.set\t";
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +000099 DwarfRequiresFrameSection = false;
100 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",%progbits";
101 DwarfInfoSection = "\t.section\t.debug_info,\"\",%progbits";
102 DwarfLineSection = "\t.section\t.debug_line,\"\",%progbits";
103 DwarfFrameSection = "\t.section\t.debug_frame,\"\",%progbits";
104 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",%progbits";
105 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",%progbits";
106 DwarfStrSection = "\t.section\t.debug_str,\"\",%progbits";
107 DwarfLocSection = "\t.section\t.debug_loc,\"\",%progbits";
108 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",%progbits";
109 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",%progbits";
110 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",%progbits";
111
Lauro Ramos Venancio6d7dd8e2007-03-05 17:59:58 +0000112 if (Subtarget->isAAPCS_ABI()) {
113 StaticCtorsSection = "\t.section .init_array,\"aw\",%init_array";
114 StaticDtorsSection = "\t.section .fini_array,\"aw\",%fini_array";
115 } else {
116 StaticCtorsSection = "\t.section .ctors,\"aw\",%progbits";
117 StaticDtorsSection = "\t.section .dtors,\"aw\",%progbits";
118 }
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000119 TLSDataSection = "\t.section .tdata,\"awT\",%progbits";
120 TLSBSSSection = "\t.section .tbss,\"awT\",%nobits";
Evan Chenga8e29892007-01-19 07:51:42 +0000121 }
Lauro Ramos Venancioea9fc582007-01-26 23:24:43 +0000122
123 ZeroDirective = "\t.space\t";
124 AlignmentIsInBytes = false;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000125 Data64bitsDirective = 0;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000126 CommentString = "@";
Evan Chenga8e29892007-01-19 07:51:42 +0000127 DataSection = "\t.data";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000128 ConstantPoolSection = "\t.text\n";
Lauro Ramos Venancioea9fc582007-01-26 23:24:43 +0000129 COMMDirectiveTakesAlignment = false;
130 InlineAsmStart = "@ InlineAsm Start";
131 InlineAsmEnd = "@ InlineAsm End";
132 LCOMMDirective = "\t.lcomm\t";
Dale Johannesen67cf5612007-05-02 01:02:40 +0000133}
134
135/// Count the number of comma-separated arguments.
136/// Do not try to detect errors.
137unsigned ARMTargetAsmInfo::countArguments(const char* p) const {
138 unsigned count = 0;
139 while (*p && isspace(*p) && *p != '\n')
140 p++;
141 count++;
142 while (*p && *p!='\n' &&
143 strncmp(p, CommentString, strlen(CommentString))!=0) {
144 if (*p==',')
145 count++;
146 p++;
147 }
148 return count;
149}
150
151/// Count the length of a string enclosed in quote characters.
152/// Do not try to detect errors.
153unsigned ARMTargetAsmInfo::countString(const char* p) const {
154 unsigned count = 0;
155 while (*p && isspace(*p) && *p!='\n')
156 p++;
157 if (!*p || *p != '\"')
158 return count;
159 while (*++p && *p != '\"')
160 count++;
161 return count;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000162}
Dale Johannesen86501992007-04-29 19:17:45 +0000163
164/// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
165unsigned ARMTargetAsmInfo::getInlineAsmLength(const char *Str) const {
166 // Count the number of bytes in the asm.
167 bool atInsnStart = true;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000168 bool inTextSection = true;
Dale Johannesen86501992007-04-29 19:17:45 +0000169 unsigned Length = 0;
170 for (; *Str; ++Str) {
171 if (atInsnStart) {
172 // Skip whitespace
173 while (*Str && isspace(*Str) && *Str != '\n')
174 Str++;
175 // Skip label
176 for (const char* p = Str; *p && !isspace(*p); p++)
177 if (*p == ':') {
178 Str = p+1;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000179 while (*Str && isspace(*Str) && *Str != '\n')
180 Str++;
Dale Johannesen86501992007-04-29 19:17:45 +0000181 break;
182 }
183 // Ignore everything from comment char(s) to EOL
184 if (strncmp(Str, CommentString, strlen(CommentString))==-0)
185 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000186 // FIXME do something like the following for non-Darwin
187 else if (*Str == '.' && Subtarget->isTargetDarwin()) {
188 // Directive.
189 atInsnStart = false;
190 // Some change the section, but don't generate code.
191 if (strncasecmp(Str, ".literal4", strlen(".literal4"))==0 ||
192 strncasecmp(Str, ".literal8", strlen(".literal8"))==0 ||
193 strncasecmp(Str, ".const", strlen(".const"))==0 ||
194 strncasecmp(Str, ".constructor", strlen(".constructor"))==0 ||
195 strncasecmp(Str, ".cstring", strlen(".cstring"))==0 ||
196 strncasecmp(Str, ".data", strlen(".data"))==0 ||
197 strncasecmp(Str, ".destructor", strlen(".destructor"))==0 ||
198 strncasecmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
199 strncasecmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
200 strncasecmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
201 strncasecmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
202 strncasecmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
203 strncasecmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
204 strncasecmp(Str, ".static_data", strlen(".static_data"))==0 ||
205 strncasecmp(Str, ".section", strlen(".section"))==0 ||
206 strncasecmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
207 strncasecmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
208 strncasecmp(Str, ".dyld", strlen(".dyld"))==0 ||
209 strncasecmp(Str, ".const_data", strlen(".const_data"))==0 ||
210 strncasecmp(Str, ".objc", strlen(".objc"))==0 || //// many directives
211 strncasecmp(Str, ".static_const", strlen(".static_const"))==0)
212 inTextSection=false;
213 else if (strncasecmp(Str, ".text", strlen(".text"))==0)
214 inTextSection = true;
215 // Some can't really be handled without implementing significant pieces
216 // of an assembler. Others require dynamic adjustment of block sizes in
217 // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
218 // instruction in there, and none of these are currently used in the kernel.
219 else if (strncasecmp(Str, ".macro", strlen(".macro"))==0 ||
220 strncasecmp(Str, ".if", strlen(".if"))==0 ||
221 strncasecmp(Str, ".align", strlen(".align"))==0 ||
222 strncasecmp(Str, ".fill", strlen(".fill"))==0 ||
223 strncasecmp(Str, ".space", strlen(".space"))==0 ||
224 strncasecmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
225 strncasecmp(Str, ".p2align", strlen(".p2align"))==0 ||
226 strncasecmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
227 strncasecmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
228 strncasecmp(Str, ".align32", strlen(".p2align32"))==0 ||
229 strncasecmp(Str, ".include", strlen(".include"))==0)
230 cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
231 " constant pools (the assembler will tell you if this happens).\n";
232 // Some generate code, but this is only interesting in the text section.
233 else if (inTextSection) {
234 if (strncasecmp(Str, ".long", strlen(".long"))==0)
235 Length += 4*countArguments(Str+strlen(".long"));
236 else if (strncasecmp(Str, ".short", strlen(".short"))==0)
237 Length += 2*countArguments(Str+strlen(".short"));
238 else if (strncasecmp(Str, ".byte", strlen(".byte"))==0)
239 Length += 1*countArguments(Str+strlen(".byte"));
240 else if (strncasecmp(Str, ".single", strlen(".single"))==0)
241 Length += 4*countArguments(Str+strlen(".single"));
242 else if (strncasecmp(Str, ".double", strlen(".double"))==0)
243 Length += 8*countArguments(Str+strlen(".double"));
244 else if (strncasecmp(Str, ".quad", strlen(".quad"))==0)
245 Length += 16*countArguments(Str+strlen(".quad"));
246 else if (strncasecmp(Str, ".ascii", strlen(".ascii"))==0)
247 Length += countString(Str+strlen(".ascii"));
248 else if (strncasecmp(Str, ".asciz", strlen(".asciz"))==0)
249 Length += countString(Str+strlen(".asciz"))+1;
250 }
251 } else if (inTextSection) {
Dale Johannesen86501992007-04-29 19:17:45 +0000252 // An instruction
253 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000254 if (Subtarget->isThumb()) {
Dale Johannesen86501992007-04-29 19:17:45 +0000255 // BL and BLX <non-reg> are 4 bytes, all others 2.
Dale Johannesen67cf5612007-05-02 01:02:40 +0000256 if (strncasecmp(Str, "blx", strlen("blx"))==0) {
257 const char* p = Str+3;
258 while (*p && isspace(*p))
259 p++;
260 if (*p == 'r' || *p=='R')
261 Length += 2; // BLX reg
Dale Johannesen86501992007-04-29 19:17:45 +0000262 else
Dale Johannesen67cf5612007-05-02 01:02:40 +0000263 Length += 4; // BLX non-reg
264 } else if (strncasecmp(Str, "bl", strlen("bl"))==0)
265 Length += 4; // BL
266 else
Dale Johannesen86501992007-04-29 19:17:45 +0000267 Length += 2; // Thumb anything else
268 }
269 else
270 Length += 4; // ARM
271 }
272 }
273 if (*Str == '\n' || *Str == SeparatorChar)
274 atInsnStart = true;
275 }
276 return Length;
277}