blob: 4777fdc70848d10199a8d157e076a1faedcd8507 [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 Korobeynikov32b952a2008-09-25 21:00:33 +000020const char *const llvm::arm_asm_table[] = {
Chris Lattnera93ca922009-06-18 23:41:35 +000021 "{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};
Lauro Ramos Venancio61718a62007-06-08 21:06:23 +000045
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
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000050 ZeroDirective = "\t.space\t";
51 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
52 SetDirective = "\t.set\t";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000053 ProtectedDirective = NULL;
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000054 HasDotTypeDotSizeDirective = false;
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000055}
56
57ARMELFTargetAsmInfo::ARMELFTargetAsmInfo(const ARMTargetMachine &TM):
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000058 ARMTargetAsmInfo<ELFTargetAsmInfo>(TM) {
Dan Gohman8f092252008-11-03 18:22:42 +000059 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000060
61 NeedsSet = false;
62 HasLEB128 = true;
63 AbsoluteDebugSectionOffsets = true;
64 CStringSection = ".rodata.str";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000065 PrivateGlobalPrefix = ".L";
66 WeakRefDirective = "\t.weak\t";
67 SetDirective = "\t.set\t";
68 DwarfRequiresFrameSection = false;
69 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",%progbits";
70 DwarfInfoSection = "\t.section\t.debug_info,\"\",%progbits";
71 DwarfLineSection = "\t.section\t.debug_line,\"\",%progbits";
72 DwarfFrameSection = "\t.section\t.debug_frame,\"\",%progbits";
73 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",%progbits";
74 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",%progbits";
75 DwarfStrSection = "\t.section\t.debug_str,\"\",%progbits";
76 DwarfLocSection = "\t.section\t.debug_loc,\"\",%progbits";
77 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",%progbits";
78 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",%progbits";
Chris Lattnerb839c3f2009-06-18 23:31:37 +000079 DwarfMacroInfoSection = "\t.section\t.debug_macinfo,\"\",%progbits";
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000080
81 if (Subtarget->isAAPCS_ABI()) {
82 StaticCtorsSection = "\t.section .init_array,\"aw\",%init_array";
83 StaticDtorsSection = "\t.section .fini_array,\"aw\",%fini_array";
84 } else {
85 StaticCtorsSection = "\t.section .ctors,\"aw\",%progbits";
86 StaticDtorsSection = "\t.section .dtors,\"aw\",%progbits";
87 }
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +000088}
89
Dale Johannesen67cf5612007-05-02 01:02:40 +000090/// Count the number of comma-separated arguments.
91/// Do not try to detect errors.
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000092template <class BaseTAI>
93unsigned ARMTargetAsmInfo<BaseTAI>::countArguments(const char* p) const {
Dale Johannesen67cf5612007-05-02 01:02:40 +000094 unsigned count = 0;
95 while (*p && isspace(*p) && *p != '\n')
96 p++;
97 count++;
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000098 while (*p && *p!='\n' &&
99 strncmp(p, BaseTAI::CommentString,
100 strlen(BaseTAI::CommentString))!=0) {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000101 if (*p==',')
102 count++;
103 p++;
104 }
105 return count;
106}
107
108/// Count the length of a string enclosed in quote characters.
109/// Do not try to detect errors.
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000110template <class BaseTAI>
111unsigned ARMTargetAsmInfo<BaseTAI>::countString(const char* p) const {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000112 unsigned count = 0;
113 while (*p && isspace(*p) && *p!='\n')
114 p++;
115 if (!*p || *p != '\"')
116 return count;
117 while (*++p && *p != '\"')
118 count++;
119 return count;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000120}
Dale Johannesen86501992007-04-29 19:17:45 +0000121
122/// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000123template <class BaseTAI>
124unsigned ARMTargetAsmInfo<BaseTAI>::getInlineAsmLength(const char *s) const {
Dale Johannesenca4571e2007-10-25 21:54:43 +0000125 // Make a lowercase-folded version of s for counting purposes.
126 char *q, *s_copy = (char *)malloc(strlen(s) + 1);
127 strcpy(s_copy, s);
128 for (q=s_copy; *q; q++)
129 *q = tolower(*q);
130 const char *Str = s_copy;
131
Dale Johannesen86501992007-04-29 19:17:45 +0000132 // Count the number of bytes in the asm.
133 bool atInsnStart = true;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000134 bool inTextSection = true;
Dale Johannesen86501992007-04-29 19:17:45 +0000135 unsigned Length = 0;
136 for (; *Str; ++Str) {
137 if (atInsnStart) {
138 // Skip whitespace
139 while (*Str && isspace(*Str) && *Str != '\n')
140 Str++;
141 // Skip label
142 for (const char* p = Str; *p && !isspace(*p); p++)
143 if (*p == ':') {
144 Str = p+1;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000145 while (*Str && isspace(*Str) && *Str != '\n')
146 Str++;
Dale Johannesen86501992007-04-29 19:17:45 +0000147 break;
148 }
Chris Lattnereda2ec32009-03-11 16:14:25 +0000149
150 if (*Str == 0) break;
151
Dale Johannesen86501992007-04-29 19:17:45 +0000152 // Ignore everything from comment char(s) to EOL
Chris Lattnereda2ec32009-03-11 16:14:25 +0000153 if (strncmp(Str, BaseTAI::CommentString,
154 strlen(BaseTAI::CommentString)) == 0)
Dale Johannesen86501992007-04-29 19:17:45 +0000155 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000156 // FIXME do something like the following for non-Darwin
157 else if (*Str == '.' && Subtarget->isTargetDarwin()) {
158 // Directive.
159 atInsnStart = false;
Dale Johannesenca4571e2007-10-25 21:54:43 +0000160
Dale Johannesen67cf5612007-05-02 01:02:40 +0000161 // Some change the section, but don't generate code.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000162 if (strncmp(Str, ".literal4", strlen(".literal4"))==0 ||
163 strncmp(Str, ".literal8", strlen(".literal8"))==0 ||
164 strncmp(Str, ".const", strlen(".const"))==0 ||
165 strncmp(Str, ".constructor", strlen(".constructor"))==0 ||
166 strncmp(Str, ".cstring", strlen(".cstring"))==0 ||
167 strncmp(Str, ".data", strlen(".data"))==0 ||
168 strncmp(Str, ".destructor", strlen(".destructor"))==0 ||
169 strncmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
170 strncmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
171 strncmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
172 strncmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
173 strncmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
174 strncmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
175 strncmp(Str, ".static_data", strlen(".static_data"))==0 ||
176 strncmp(Str, ".section", strlen(".section"))==0 ||
177 strncmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
178 strncmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
179 strncmp(Str, ".dyld", strlen(".dyld"))==0 ||
180 strncmp(Str, ".const_data", strlen(".const_data"))==0 ||
181 strncmp(Str, ".objc", strlen(".objc"))==0 || //// many directives
182 strncmp(Str, ".static_const", strlen(".static_const"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000183 inTextSection=false;
Dale Johannesenca4571e2007-10-25 21:54:43 +0000184 else if (strncmp(Str, ".text", strlen(".text"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000185 inTextSection = true;
186 // Some can't really be handled without implementing significant pieces
187 // of an assembler. Others require dynamic adjustment of block sizes in
188 // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
189 // instruction in there, and none of these are currently used in the kernel.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000190 else if (strncmp(Str, ".macro", strlen(".macro"))==0 ||
191 strncmp(Str, ".if", strlen(".if"))==0 ||
192 strncmp(Str, ".align", strlen(".align"))==0 ||
193 strncmp(Str, ".fill", strlen(".fill"))==0 ||
194 strncmp(Str, ".space", strlen(".space"))==0 ||
195 strncmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
196 strncmp(Str, ".p2align", strlen(".p2align"))==0 ||
197 strncmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
198 strncmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
199 strncmp(Str, ".align32", strlen(".p2align32"))==0 ||
200 strncmp(Str, ".include", strlen(".include"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000201 cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
202 " constant pools (the assembler will tell you if this happens).\n";
203 // Some generate code, but this is only interesting in the text section.
204 else if (inTextSection) {
Dale Johannesenca4571e2007-10-25 21:54:43 +0000205 if (strncmp(Str, ".long", strlen(".long"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000206 Length += 4*countArguments(Str+strlen(".long"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000207 else if (strncmp(Str, ".short", strlen(".short"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000208 Length += 2*countArguments(Str+strlen(".short"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000209 else if (strncmp(Str, ".byte", strlen(".byte"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000210 Length += 1*countArguments(Str+strlen(".byte"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000211 else if (strncmp(Str, ".single", strlen(".single"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000212 Length += 4*countArguments(Str+strlen(".single"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000213 else if (strncmp(Str, ".double", strlen(".double"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000214 Length += 8*countArguments(Str+strlen(".double"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000215 else if (strncmp(Str, ".quad", strlen(".quad"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000216 Length += 16*countArguments(Str+strlen(".quad"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000217 else if (strncmp(Str, ".ascii", strlen(".ascii"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000218 Length += countString(Str+strlen(".ascii"));
Dale Johannesenca4571e2007-10-25 21:54:43 +0000219 else if (strncmp(Str, ".asciz", strlen(".asciz"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000220 Length += countString(Str+strlen(".asciz"))+1;
221 }
222 } else if (inTextSection) {
Dale Johannesen86501992007-04-29 19:17:45 +0000223 // An instruction
224 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000225 if (Subtarget->isThumb()) {
Dale Johannesen86501992007-04-29 19:17:45 +0000226 // BL and BLX <non-reg> are 4 bytes, all others 2.
Dale Johannesenca4571e2007-10-25 21:54:43 +0000227 if (strncmp(Str, "blx", strlen("blx"))==0) {
Dale Johannesen67cf5612007-05-02 01:02:40 +0000228 const char* p = Str+3;
229 while (*p && isspace(*p))
230 p++;
231 if (*p == 'r' || *p=='R')
232 Length += 2; // BLX reg
Dale Johannesen86501992007-04-29 19:17:45 +0000233 else
Dale Johannesen67cf5612007-05-02 01:02:40 +0000234 Length += 4; // BLX non-reg
Dale Johannesenca4571e2007-10-25 21:54:43 +0000235 } else if (strncmp(Str, "bl", strlen("bl"))==0)
Dale Johannesen67cf5612007-05-02 01:02:40 +0000236 Length += 4; // BL
237 else
Dale Johannesen86501992007-04-29 19:17:45 +0000238 Length += 2; // Thumb anything else
239 }
240 else
241 Length += 4; // ARM
242 }
243 }
Anton Korobeynikov32b952a2008-09-25 21:00:33 +0000244 if (*Str == '\n' || *Str == BaseTAI::SeparatorChar)
Dale Johannesen86501992007-04-29 19:17:45 +0000245 atInsnStart = true;
246 }
Dale Johannesenca4571e2007-10-25 21:54:43 +0000247 free(s_copy);
Dale Johannesen86501992007-04-29 19:17:45 +0000248 return Length;
249}
Anton Korobeynikov6381a132008-10-05 08:53:29 +0000250
251// Instantiate default implementation.
252TEMPLATE_INSTANTIATION(class ARMTargetAsmInfo<TargetAsmInfo>);