blob: 61313c56852863511ee49c062d02885374ad0dd0 [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
Lauro Ramos Venancio61718a62007-06-08 21:06:23 +000020static const char* arm_asm_table[] = {"{r0}", "r0",
21 "{r1}", "r1",
22 "{r2}", "r2",
23 "{r3}", "r3",
24 "{r4}", "r4",
25 "{r5}", "r5",
26 "{r6}", "r6",
27 "{r7}", "r7",
28 "{r8}", "r8",
29 "{r9}", "r9",
30 "{r10}", "r10",
31 "{r11}", "r11",
32 "{r12}", "r12",
33 "{r13}", "r13",
34 "{r14}", "r14",
35 "{lr}", "lr",
36 "{sp}", "sp",
37 "{ip}", "ip",
38 "{fp}", "fp",
39 "{sl}", "sl",
40 "{memory}", "memory",
41 "{cc}", "cc",
42 0,0};
43
Jim Laskey8e8de8f2006-09-07 22:05:02 +000044ARMTargetAsmInfo::ARMTargetAsmInfo(const ARMTargetMachine &TM) {
Dale Johannesen67cf5612007-05-02 01:02:40 +000045 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Lauro Ramos Venancio61718a62007-06-08 21:06:23 +000046 AsmTransCBE = arm_asm_table;
Evan Chenge433ea92007-01-19 19:23:47 +000047 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +000048 GlobalPrefix = "_";
Evan Chenge433ea92007-01-19 19:23:47 +000049 PrivateGlobalPrefix = "L";
50 BSSSection = 0; // no BSS section.
51 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
Evan Chenga8e29892007-01-19 07:51:42 +000052 SetDirective = "\t.set";
53 WeakRefDirective = "\t.weak_reference\t";
Evan Chenga6f567c2007-01-23 19:06:03 +000054 HiddenDirective = "\t.private_extern\t";
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +000055 ProtectedDirective = NULL;
Evan Chenga8e29892007-01-19 07:51:42 +000056 JumpTableDataSection = ".const";
57 CStringSection = "\t.cstring";
Evan Cheng98ded762007-03-08 01:25:25 +000058 FourByteConstantSection = "\t.literal4\n";
59 EightByteConstantSection = "\t.literal8\n";
60 ReadOnlySection = "\t.const\n";
Evan Chenge433ea92007-01-19 19:23:47 +000061 HasDotTypeDotSizeDirective = false;
Bill Wendlingfe4afb12007-09-11 23:55:40 +000062 NeedsIndirectEncoding = true;
Evan Chengb267ca12007-01-30 08:04:53 +000063 if (TM.getRelocationModel() == Reloc::Static) {
64 StaticCtorsSection = ".constructor";
65 StaticDtorsSection = ".destructor";
66 } else {
67 StaticCtorsSection = ".mod_init_func";
68 StaticDtorsSection = ".mod_term_func";
69 }
Evan Chenga8e29892007-01-19 07:51:42 +000070
Evan Cheng8e1185b2007-01-19 18:59:56 +000071 // In non-PIC modes, emit a special label before jump tables so that the
72 // linker can perform more accurate dead code stripping.
73 if (TM.getRelocationModel() != Reloc::PIC_) {
74 // Emit a local label that is preserved until the linker runs.
75 JumpTableSpecialLabelPrefix = "l";
76 }
77
Evan Chenga8e29892007-01-19 07:51:42 +000078 NeedsSet = true;
79 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
80 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
81 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
82 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
83 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
84 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
85 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
86 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
87 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
88 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
89 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
90 } else {
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +000091 NeedsSet = false;
92 HasLEB128 = true;
93 AbsoluteDebugSectionOffsets = true;
94 ReadOnlySection = "\t.section\t.rodata\n";
Lauro Ramos Venancioc33f6742007-02-01 21:43:53 +000095 PrivateGlobalPrefix = ".L";
Evan Chenga8e29892007-01-19 07:51:42 +000096 WeakRefDirective = "\t.weak\t";
Lauro Ramos Venancio69642f12007-05-14 18:32:56 +000097 SetDirective = "\t.set\t";
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +000098 DwarfRequiresFrameSection = false;
99 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",%progbits";
100 DwarfInfoSection = "\t.section\t.debug_info,\"\",%progbits";
101 DwarfLineSection = "\t.section\t.debug_line,\"\",%progbits";
102 DwarfFrameSection = "\t.section\t.debug_frame,\"\",%progbits";
103 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",%progbits";
104 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",%progbits";
105 DwarfStrSection = "\t.section\t.debug_str,\"\",%progbits";
106 DwarfLocSection = "\t.section\t.debug_loc,\"\",%progbits";
107 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",%progbits";
108 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",%progbits";
109 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",%progbits";
110
Lauro Ramos Venancio6d7dd8e2007-03-05 17:59:58 +0000111 if (Subtarget->isAAPCS_ABI()) {
112 StaticCtorsSection = "\t.section .init_array,\"aw\",%init_array";
113 StaticDtorsSection = "\t.section .fini_array,\"aw\",%fini_array";
114 } else {
115 StaticCtorsSection = "\t.section .ctors,\"aw\",%progbits";
116 StaticDtorsSection = "\t.section .dtors,\"aw\",%progbits";
117 }
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000118 TLSDataSection = "\t.section .tdata,\"awT\",%progbits";
119 TLSBSSSection = "\t.section .tbss,\"awT\",%nobits";
Evan Chenga8e29892007-01-19 07:51:42 +0000120 }
Lauro Ramos Venancioea9fc582007-01-26 23:24:43 +0000121
122 ZeroDirective = "\t.space\t";
123 AlignmentIsInBytes = false;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000124 Data64bitsDirective = 0;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000125 CommentString = "@";
Evan Chenga8e29892007-01-19 07:51:42 +0000126 DataSection = "\t.data";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000127 ConstantPoolSection = "\t.text\n";
Lauro Ramos Venancioea9fc582007-01-26 23:24:43 +0000128 COMMDirectiveTakesAlignment = false;
129 InlineAsmStart = "@ InlineAsm Start";
130 InlineAsmEnd = "@ InlineAsm End";
131 LCOMMDirective = "\t.lcomm\t";
Dale Johannesen67cf5612007-05-02 01:02:40 +0000132}
133
134/// Count the number of comma-separated arguments.
135/// Do not try to detect errors.
136unsigned ARMTargetAsmInfo::countArguments(const char* p) const {
137 unsigned count = 0;
138 while (*p && isspace(*p) && *p != '\n')
139 p++;
140 count++;
141 while (*p && *p!='\n' &&
142 strncmp(p, CommentString, strlen(CommentString))!=0) {
143 if (*p==',')
144 count++;
145 p++;
146 }
147 return count;
148}
149
150/// Count the length of a string enclosed in quote characters.
151/// Do not try to detect errors.
152unsigned ARMTargetAsmInfo::countString(const char* p) const {
153 unsigned count = 0;
154 while (*p && isspace(*p) && *p!='\n')
155 p++;
156 if (!*p || *p != '\"')
157 return count;
158 while (*++p && *p != '\"')
159 count++;
160 return count;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000161}
Dale Johannesen86501992007-04-29 19:17:45 +0000162
163/// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000164unsigned ARMTargetAsmInfo::getInlineAsmLength(const char *s) const {
165 // Make a lowercase-folded version of s for counting purposes.
166 char *q, *s_copy = (char *)malloc(strlen(s) + 1);
167 strcpy(s_copy, s);
168 for (q=s_copy; *q; q++)
169 *q = tolower(*q);
170 const char *Str = s_copy;
171
Dale Johannesen86501992007-04-29 19:17:45 +0000172 // Count the number of bytes in the asm.
173 bool atInsnStart = true;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000174 bool inTextSection = true;
Dale Johannesen86501992007-04-29 19:17:45 +0000175 unsigned Length = 0;
176 for (; *Str; ++Str) {
177 if (atInsnStart) {
178 // Skip whitespace
179 while (*Str && isspace(*Str) && *Str != '\n')
180 Str++;
181 // Skip label
182 for (const char* p = Str; *p && !isspace(*p); p++)
183 if (*p == ':') {
184 Str = p+1;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000185 while (*Str && isspace(*Str) && *Str != '\n')
186 Str++;
Dale Johannesen86501992007-04-29 19:17:45 +0000187 break;
188 }
189 // Ignore everything from comment char(s) to EOL
190 if (strncmp(Str, CommentString, strlen(CommentString))==-0)
191 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000192 // FIXME do something like the following for non-Darwin
193 else if (*Str == '.' && Subtarget->isTargetDarwin()) {
194 // Directive.
195 atInsnStart = false;
Dale Johannesenca4571e2007-10-25 21:54:43 +0000196
Dale Johannesen67cf5612007-05-02 01:02:40 +0000197 // Some change the section, but don't generate code.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000198 if (strncmp(Str, ".literal4", strlen(".literal4"))==0 ||
199 strncmp(Str, ".literal8", strlen(".literal8"))==0 ||
200 strncmp(Str, ".const", strlen(".const"))==0 ||
201 strncmp(Str, ".constructor", strlen(".constructor"))==0 ||
202 strncmp(Str, ".cstring", strlen(".cstring"))==0 ||
203 strncmp(Str, ".data", strlen(".data"))==0 ||
204 strncmp(Str, ".destructor", strlen(".destructor"))==0 ||
205 strncmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
206 strncmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
207 strncmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
208 strncmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
209 strncmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
210 strncmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
211 strncmp(Str, ".static_data", strlen(".static_data"))==0 ||
212 strncmp(Str, ".section", strlen(".section"))==0 ||
213 strncmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
214 strncmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
215 strncmp(Str, ".dyld", strlen(".dyld"))==0 ||
216 strncmp(Str, ".const_data", strlen(".const_data"))==0 ||
217 strncmp(Str, ".objc", strlen(".objc"))==0 || //// many directives
218 strncmp(Str, ".static_const", strlen(".static_const"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000219 inTextSection=false;
Dale Johannesenca4571e2007-10-25 21:54:43 +0000220 else if (strncmp(Str, ".text", strlen(".text"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000221 inTextSection = true;
222 // Some can't really be handled without implementing significant pieces
223 // of an assembler. Others require dynamic adjustment of block sizes in
224 // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
225 // instruction in there, and none of these are currently used in the kernel.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000226 else if (strncmp(Str, ".macro", strlen(".macro"))==0 ||
227 strncmp(Str, ".if", strlen(".if"))==0 ||
228 strncmp(Str, ".align", strlen(".align"))==0 ||
229 strncmp(Str, ".fill", strlen(".fill"))==0 ||
230 strncmp(Str, ".space", strlen(".space"))==0 ||
231 strncmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
232 strncmp(Str, ".p2align", strlen(".p2align"))==0 ||
233 strncmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
234 strncmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
235 strncmp(Str, ".align32", strlen(".p2align32"))==0 ||
236 strncmp(Str, ".include", strlen(".include"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000237 cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
238 " constant pools (the assembler will tell you if this happens).\n";
239 // Some generate code, but this is only interesting in the text section.
240 else if (inTextSection) {
Dale Johannesenca4571e2007-10-25 21:54:43 +0000241 if (strncmp(Str, ".long", strlen(".long"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000242 Length += 4*countArguments(Str+strlen(".long"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000243 else if (strncmp(Str, ".short", strlen(".short"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000244 Length += 2*countArguments(Str+strlen(".short"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000245 else if (strncmp(Str, ".byte", strlen(".byte"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000246 Length += 1*countArguments(Str+strlen(".byte"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000247 else if (strncmp(Str, ".single", strlen(".single"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000248 Length += 4*countArguments(Str+strlen(".single"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000249 else if (strncmp(Str, ".double", strlen(".double"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000250 Length += 8*countArguments(Str+strlen(".double"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000251 else if (strncmp(Str, ".quad", strlen(".quad"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000252 Length += 16*countArguments(Str+strlen(".quad"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000253 else if (strncmp(Str, ".ascii", strlen(".ascii"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000254 Length += countString(Str+strlen(".ascii"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000255 else if (strncmp(Str, ".asciz", strlen(".asciz"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000256 Length += countString(Str+strlen(".asciz"))+1;
257 }
258 } else if (inTextSection) {
Dale Johannesen86501992007-04-29 19:17:45 +0000259 // An instruction
260 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000261 if (Subtarget->isThumb()) {
Dale Johannesen86501992007-04-29 19:17:45 +0000262 // BL and BLX <non-reg> are 4 bytes, all others 2.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000263 if (strncmp(Str, "blx", strlen("blx"))==0) {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000264 const char* p = Str+3;
265 while (*p && isspace(*p))
266 p++;
267 if (*p == 'r' || *p=='R')
268 Length += 2; // BLX reg
Dale Johannesen86501992007-04-29 19:17:45 +0000269 else
Dale Johannesen67cf5612007-05-02 01:02:40 +0000270 Length += 4; // BLX non-reg
Dale Johannesenca4571e2007-10-25 21:54:43 +0000271 } else if (strncmp(Str, "bl", strlen("bl"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000272 Length += 4; // BL
273 else
Dale Johannesen86501992007-04-29 19:17:45 +0000274 Length += 2; // Thumb anything else
275 }
276 else
277 Length += 4; // ARM
278 }
279 }
280 if (*Str == '\n' || *Str == SeparatorChar)
281 atInsnStart = true;
282 }
Dale Johannesenca4571e2007-10-25 21:54:43 +0000283 free(s_copy);
Dale Johannesen86501992007-04-29 19:17:45 +0000284 return Length;
285}