blob: 13b9362a55f1bc61616d7e3ed64363b3a5ce90c0 [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
21ARMTargetAsmInfo::ARMTargetAsmInfo(const ARMTargetMachine &TM) {
Dale Johannesen67cf5612007-05-02 01:02:40 +000022 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Chenge433ea92007-01-19 19:23:47 +000023 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +000024 GlobalPrefix = "_";
Evan Chenge433ea92007-01-19 19:23:47 +000025 PrivateGlobalPrefix = "L";
26 BSSSection = 0; // no BSS section.
27 ZeroFillDirective = "\t.zerofill\t"; // Uses .zerofill
Evan Chenga8e29892007-01-19 07:51:42 +000028 SetDirective = "\t.set";
29 WeakRefDirective = "\t.weak_reference\t";
Evan Chenga6f567c2007-01-23 19:06:03 +000030 HiddenDirective = "\t.private_extern\t";
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +000031 ProtectedDirective = NULL;
Evan Chenga8e29892007-01-19 07:51:42 +000032 JumpTableDataSection = ".const";
33 CStringSection = "\t.cstring";
Evan Cheng98ded762007-03-08 01:25:25 +000034 FourByteConstantSection = "\t.literal4\n";
35 EightByteConstantSection = "\t.literal8\n";
36 ReadOnlySection = "\t.const\n";
Evan Chenge433ea92007-01-19 19:23:47 +000037 HasDotTypeDotSizeDirective = false;
Evan Chengb267ca12007-01-30 08:04:53 +000038 if (TM.getRelocationModel() == Reloc::Static) {
39 StaticCtorsSection = ".constructor";
40 StaticDtorsSection = ".destructor";
41 } else {
42 StaticCtorsSection = ".mod_init_func";
43 StaticDtorsSection = ".mod_term_func";
44 }
Evan Chenga8e29892007-01-19 07:51:42 +000045
Evan Cheng8e1185b2007-01-19 18:59:56 +000046 // In non-PIC modes, emit a special label before jump tables so that the
47 // linker can perform more accurate dead code stripping.
48 if (TM.getRelocationModel() != Reloc::PIC_) {
49 // Emit a local label that is preserved until the linker runs.
50 JumpTableSpecialLabelPrefix = "l";
51 }
52
Evan Chenga8e29892007-01-19 07:51:42 +000053 NeedsSet = true;
54 DwarfAbbrevSection = ".section __DWARF,__debug_abbrev,regular,debug";
55 DwarfInfoSection = ".section __DWARF,__debug_info,regular,debug";
56 DwarfLineSection = ".section __DWARF,__debug_line,regular,debug";
57 DwarfFrameSection = ".section __DWARF,__debug_frame,regular,debug";
58 DwarfPubNamesSection = ".section __DWARF,__debug_pubnames,regular,debug";
59 DwarfPubTypesSection = ".section __DWARF,__debug_pubtypes,regular,debug";
60 DwarfStrSection = ".section __DWARF,__debug_str,regular,debug";
61 DwarfLocSection = ".section __DWARF,__debug_loc,regular,debug";
62 DwarfARangesSection = ".section __DWARF,__debug_aranges,regular,debug";
63 DwarfRangesSection = ".section __DWARF,__debug_ranges,regular,debug";
64 DwarfMacInfoSection = ".section __DWARF,__debug_macinfo,regular,debug";
65 } else {
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +000066 NeedsSet = false;
67 HasLEB128 = true;
68 AbsoluteDebugSectionOffsets = true;
69 ReadOnlySection = "\t.section\t.rodata\n";
Lauro Ramos Venancioc33f6742007-02-01 21:43:53 +000070 PrivateGlobalPrefix = ".L";
Evan Chenga8e29892007-01-19 07:51:42 +000071 WeakRefDirective = "\t.weak\t";
Lauro Ramos Venancio69642f12007-05-14 18:32:56 +000072 SetDirective = "\t.set\t";
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +000073 DwarfRequiresFrameSection = false;
74 DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",%progbits";
75 DwarfInfoSection = "\t.section\t.debug_info,\"\",%progbits";
76 DwarfLineSection = "\t.section\t.debug_line,\"\",%progbits";
77 DwarfFrameSection = "\t.section\t.debug_frame,\"\",%progbits";
78 DwarfPubNamesSection ="\t.section\t.debug_pubnames,\"\",%progbits";
79 DwarfPubTypesSection ="\t.section\t.debug_pubtypes,\"\",%progbits";
80 DwarfStrSection = "\t.section\t.debug_str,\"\",%progbits";
81 DwarfLocSection = "\t.section\t.debug_loc,\"\",%progbits";
82 DwarfARangesSection = "\t.section\t.debug_aranges,\"\",%progbits";
83 DwarfRangesSection = "\t.section\t.debug_ranges,\"\",%progbits";
84 DwarfMacInfoSection = "\t.section\t.debug_macinfo,\"\",%progbits";
85
Lauro Ramos Venancio6d7dd8e2007-03-05 17:59:58 +000086 if (Subtarget->isAAPCS_ABI()) {
87 StaticCtorsSection = "\t.section .init_array,\"aw\",%init_array";
88 StaticDtorsSection = "\t.section .fini_array,\"aw\",%fini_array";
89 } else {
90 StaticCtorsSection = "\t.section .ctors,\"aw\",%progbits";
91 StaticDtorsSection = "\t.section .dtors,\"aw\",%progbits";
92 }
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +000093 TLSDataSection = "\t.section .tdata,\"awT\",%progbits";
94 TLSBSSSection = "\t.section .tbss,\"awT\",%nobits";
Evan Chenga8e29892007-01-19 07:51:42 +000095 }
Lauro Ramos Venancioea9fc582007-01-26 23:24:43 +000096
97 ZeroDirective = "\t.space\t";
98 AlignmentIsInBytes = false;
Jim Laskey8e8de8f2006-09-07 22:05:02 +000099 Data64bitsDirective = 0;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000100 CommentString = "@";
Evan Chenga8e29892007-01-19 07:51:42 +0000101 DataSection = "\t.data";
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000102 ConstantPoolSection = "\t.text\n";
Lauro Ramos Venancioea9fc582007-01-26 23:24:43 +0000103 COMMDirectiveTakesAlignment = false;
104 InlineAsmStart = "@ InlineAsm Start";
105 InlineAsmEnd = "@ InlineAsm End";
106 LCOMMDirective = "\t.lcomm\t";
Dale Johannesen67cf5612007-05-02 01:02:40 +0000107}
108
109/// Count the number of comma-separated arguments.
110/// Do not try to detect errors.
111unsigned ARMTargetAsmInfo::countArguments(const char* p) const {
112 unsigned count = 0;
113 while (*p && isspace(*p) && *p != '\n')
114 p++;
115 count++;
116 while (*p && *p!='\n' &&
117 strncmp(p, CommentString, strlen(CommentString))!=0) {
118 if (*p==',')
119 count++;
120 p++;
121 }
122 return count;
123}
124
125/// Count the length of a string enclosed in quote characters.
126/// Do not try to detect errors.
127unsigned ARMTargetAsmInfo::countString(const char* p) const {
128 unsigned count = 0;
129 while (*p && isspace(*p) && *p!='\n')
130 p++;
131 if (!*p || *p != '\"')
132 return count;
133 while (*++p && *p != '\"')
134 count++;
135 return count;
Jim Laskey8e8de8f2006-09-07 22:05:02 +0000136}
Dale Johannesen86501992007-04-29 19:17:45 +0000137
138/// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
139unsigned ARMTargetAsmInfo::getInlineAsmLength(const char *Str) const {
140 // Count the number of bytes in the asm.
141 bool atInsnStart = true;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000142 bool inTextSection = true;
Dale Johannesen86501992007-04-29 19:17:45 +0000143 unsigned Length = 0;
144 for (; *Str; ++Str) {
145 if (atInsnStart) {
146 // Skip whitespace
147 while (*Str && isspace(*Str) && *Str != '\n')
148 Str++;
149 // Skip label
150 for (const char* p = Str; *p && !isspace(*p); p++)
151 if (*p == ':') {
152 Str = p+1;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000153 while (*Str && isspace(*Str) && *Str != '\n')
154 Str++;
Dale Johannesen86501992007-04-29 19:17:45 +0000155 break;
156 }
157 // Ignore everything from comment char(s) to EOL
158 if (strncmp(Str, CommentString, strlen(CommentString))==-0)
159 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000160 // FIXME do something like the following for non-Darwin
161 else if (*Str == '.' && Subtarget->isTargetDarwin()) {
162 // Directive.
163 atInsnStart = false;
164 // Some change the section, but don't generate code.
165 if (strncasecmp(Str, ".literal4", strlen(".literal4"))==0 ||
166 strncasecmp(Str, ".literal8", strlen(".literal8"))==0 ||
167 strncasecmp(Str, ".const", strlen(".const"))==0 ||
168 strncasecmp(Str, ".constructor", strlen(".constructor"))==0 ||
169 strncasecmp(Str, ".cstring", strlen(".cstring"))==0 ||
170 strncasecmp(Str, ".data", strlen(".data"))==0 ||
171 strncasecmp(Str, ".destructor", strlen(".destructor"))==0 ||
172 strncasecmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
173 strncasecmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
174 strncasecmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
175 strncasecmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
176 strncasecmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
177 strncasecmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
178 strncasecmp(Str, ".static_data", strlen(".static_data"))==0 ||
179 strncasecmp(Str, ".section", strlen(".section"))==0 ||
180 strncasecmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
181 strncasecmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
182 strncasecmp(Str, ".dyld", strlen(".dyld"))==0 ||
183 strncasecmp(Str, ".const_data", strlen(".const_data"))==0 ||
184 strncasecmp(Str, ".objc", strlen(".objc"))==0 || //// many directives
185 strncasecmp(Str, ".static_const", strlen(".static_const"))==0)
186 inTextSection=false;
187 else if (strncasecmp(Str, ".text", strlen(".text"))==0)
188 inTextSection = true;
189 // Some can't really be handled without implementing significant pieces
190 // of an assembler. Others require dynamic adjustment of block sizes in
191 // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
192 // instruction in there, and none of these are currently used in the kernel.
193 else if (strncasecmp(Str, ".macro", strlen(".macro"))==0 ||
194 strncasecmp(Str, ".if", strlen(".if"))==0 ||
195 strncasecmp(Str, ".align", strlen(".align"))==0 ||
196 strncasecmp(Str, ".fill", strlen(".fill"))==0 ||
197 strncasecmp(Str, ".space", strlen(".space"))==0 ||
198 strncasecmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
199 strncasecmp(Str, ".p2align", strlen(".p2align"))==0 ||
200 strncasecmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
201 strncasecmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
202 strncasecmp(Str, ".align32", strlen(".p2align32"))==0 ||
203 strncasecmp(Str, ".include", strlen(".include"))==0)
204 cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
205 " constant pools (the assembler will tell you if this happens).\n";
206 // Some generate code, but this is only interesting in the text section.
207 else if (inTextSection) {
208 if (strncasecmp(Str, ".long", strlen(".long"))==0)
209 Length += 4*countArguments(Str+strlen(".long"));
210 else if (strncasecmp(Str, ".short", strlen(".short"))==0)
211 Length += 2*countArguments(Str+strlen(".short"));
212 else if (strncasecmp(Str, ".byte", strlen(".byte"))==0)
213 Length += 1*countArguments(Str+strlen(".byte"));
214 else if (strncasecmp(Str, ".single", strlen(".single"))==0)
215 Length += 4*countArguments(Str+strlen(".single"));
216 else if (strncasecmp(Str, ".double", strlen(".double"))==0)
217 Length += 8*countArguments(Str+strlen(".double"));
218 else if (strncasecmp(Str, ".quad", strlen(".quad"))==0)
219 Length += 16*countArguments(Str+strlen(".quad"));
220 else if (strncasecmp(Str, ".ascii", strlen(".ascii"))==0)
221 Length += countString(Str+strlen(".ascii"));
222 else if (strncasecmp(Str, ".asciz", strlen(".asciz"))==0)
223 Length += countString(Str+strlen(".asciz"))+1;
224 }
225 } else if (inTextSection) {
Dale Johannesen86501992007-04-29 19:17:45 +0000226 // An instruction
227 atInsnStart = false;
Dale Johannesen67cf5612007-05-02 01:02:40 +0000228 if (Subtarget->isThumb()) {
Dale Johannesen86501992007-04-29 19:17:45 +0000229 // BL and BLX <non-reg> are 4 bytes, all others 2.
Dale Johannesen67cf5612007-05-02 01:02:40 +0000230 if (strncasecmp(Str, "blx", strlen("blx"))==0) {
231 const char* p = Str+3;
232 while (*p && isspace(*p))
233 p++;
234 if (*p == 'r' || *p=='R')
235 Length += 2; // BLX reg
Dale Johannesen86501992007-04-29 19:17:45 +0000236 else
Dale Johannesen67cf5612007-05-02 01:02:40 +0000237 Length += 4; // BLX non-reg
238 } else if (strncasecmp(Str, "bl", strlen("bl"))==0)
239 Length += 4; // BL
240 else
Dale Johannesen86501992007-04-29 19:17:45 +0000241 Length += 2; // Thumb anything else
242 }
243 else
244 Length += 4; // ARM
245 }
246 }
247 if (*Str == '\n' || *Str == SeparatorChar)
248 atInsnStart = true;
249 }
250 return Length;
251}