llvm.org GIT mirror llvm / 67cf561
Add some support for (Darwin) code-generating directives in getInlineAsmLength. Support is incomplete, but more accurate than gcc's. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36634 91177308-0d34-0410-b5e6-96231b3b80d8 Dale Johannesen 13 years ago
2 changed file(s) with 115 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
0
1 //===-- ARMTargetAsmInfo.cpp - ARM asm properties ---------------*- C++ -*-===//
12 //
23 // The LLVM Compiler Infrastructure
1718 using namespace llvm;
1819
1920 ARMTargetAsmInfo::ARMTargetAsmInfo(const ARMTargetMachine &TM) {
20 const ARMSubtarget *Subtarget = &TM.getSubtarget();
21 Subtarget = &TM.getSubtarget();
2122 if (Subtarget->isTargetDarwin()) {
2223 GlobalPrefix = "_";
2324 PrivateGlobalPrefix = "L";
8485 InlineAsmStart = "@ InlineAsm Start";
8586 InlineAsmEnd = "@ InlineAsm End";
8687 LCOMMDirective = "\t.lcomm\t";
87 isThumb = Subtarget->isThumb();
88 }
89
90 /// Count the number of comma-separated arguments.
91 /// Do not try to detect errors.
92 unsigned ARMTargetAsmInfo::countArguments(const char* p) const {
93 unsigned count = 0;
94 while (*p && isspace(*p) && *p != '\n')
95 p++;
96 count++;
97 while (*p && *p!='\n' &&
98 strncmp(p, CommentString, strlen(CommentString))!=0) {
99 if (*p==',')
100 count++;
101 p++;
102 }
103 return count;
104 }
105
106 /// Count the length of a string enclosed in quote characters.
107 /// Do not try to detect errors.
108 unsigned ARMTargetAsmInfo::countString(const char* p) const {
109 unsigned count = 0;
110 while (*p && isspace(*p) && *p!='\n')
111 p++;
112 if (!*p || *p != '\"')
113 return count;
114 while (*++p && *p != '\"')
115 count++;
116 return count;
88117 }
89118
90119 /// ARM-specific version of TargetAsmInfo::getInlineAsmLength.
91120 unsigned ARMTargetAsmInfo::getInlineAsmLength(const char *Str) const {
92121 // Count the number of bytes in the asm.
93122 bool atInsnStart = true;
123 bool inTextSection = true;
94124 unsigned Length = 0;
95125 for (; *Str; ++Str) {
96126 if (atInsnStart) {
101131 for (const char* p = Str; *p && !isspace(*p); p++)
102132 if (*p == ':') {
103133 Str = p+1;
134 while (*Str && isspace(*Str) && *Str != '\n')
135 Str++;
104136 break;
105137 }
106138 // Ignore everything from comment char(s) to EOL
107139 if (strncmp(Str, CommentString, strlen(CommentString))==-0)
108140 atInsnStart = false;
109 else {
141 // FIXME do something like the following for non-Darwin
142 else if (*Str == '.' && Subtarget->isTargetDarwin()) {
143 // Directive.
144 atInsnStart = false;
145 // Some change the section, but don't generate code.
146 if (strncasecmp(Str, ".literal4", strlen(".literal4"))==0 ||
147 strncasecmp(Str, ".literal8", strlen(".literal8"))==0 ||
148 strncasecmp(Str, ".const", strlen(".const"))==0 ||
149 strncasecmp(Str, ".constructor", strlen(".constructor"))==0 ||
150 strncasecmp(Str, ".cstring", strlen(".cstring"))==0 ||
151 strncasecmp(Str, ".data", strlen(".data"))==0 ||
152 strncasecmp(Str, ".destructor", strlen(".destructor"))==0 ||
153 strncasecmp(Str, ".fvmlib_init0", strlen(".fvmlib_init0"))==0 ||
154 strncasecmp(Str, ".fvmlib_init1", strlen(".fvmlib_init1"))==0 ||
155 strncasecmp(Str, ".mod_init_func", strlen(".mod_init_func"))==0 ||
156 strncasecmp(Str, ".mod_term_func", strlen(".mod_term_func"))==0 ||
157 strncasecmp(Str, ".picsymbol_stub", strlen(".picsymbol_stub"))==0 ||
158 strncasecmp(Str, ".symbol_stub", strlen(".symbol_stub"))==0 ||
159 strncasecmp(Str, ".static_data", strlen(".static_data"))==0 ||
160 strncasecmp(Str, ".section", strlen(".section"))==0 ||
161 strncasecmp(Str, ".lazy_symbol_pointer", strlen(".lazy_symbol_pointer"))==0 ||
162 strncasecmp(Str, ".non_lazy_symbol_pointer", strlen(".non_lazy_symbol_pointer"))==0 ||
163 strncasecmp(Str, ".dyld", strlen(".dyld"))==0 ||
164 strncasecmp(Str, ".const_data", strlen(".const_data"))==0 ||
165 strncasecmp(Str, ".objc", strlen(".objc"))==0 || //// many directives
166 strncasecmp(Str, ".static_const", strlen(".static_const"))==0)
167 inTextSection=false;
168 else if (strncasecmp(Str, ".text", strlen(".text"))==0)
169 inTextSection = true;
170 // Some can't really be handled without implementing significant pieces
171 // of an assembler. Others require dynamic adjustment of block sizes in
172 // AdjustBBOffsetsAfter; it's a big compile-time speed hit to check every
173 // instruction in there, and none of these are currently used in the kernel.
174 else if (strncasecmp(Str, ".macro", strlen(".macro"))==0 ||
175 strncasecmp(Str, ".if", strlen(".if"))==0 ||
176 strncasecmp(Str, ".align", strlen(".align"))==0 ||
177 strncasecmp(Str, ".fill", strlen(".fill"))==0 ||
178 strncasecmp(Str, ".space", strlen(".space"))==0 ||
179 strncasecmp(Str, ".zerofill", strlen(".zerofill"))==0 ||
180 strncasecmp(Str, ".p2align", strlen(".p2align"))==0 ||
181 strncasecmp(Str, ".p2alignw", strlen(".p2alignw"))==0 ||
182 strncasecmp(Str, ".p2alignl", strlen(".p2alignl"))==0 ||
183 strncasecmp(Str, ".align32", strlen(".p2align32"))==0 ||
184 strncasecmp(Str, ".include", strlen(".include"))==0)
185 cerr << "Directive " << Str << " in asm may lead to invalid offsets for" <<
186 " constant pools (the assembler will tell you if this happens).\n";
187 // Some generate code, but this is only interesting in the text section.
188 else if (inTextSection) {
189 if (strncasecmp(Str, ".long", strlen(".long"))==0)
190 Length += 4*countArguments(Str+strlen(".long"));
191 else if (strncasecmp(Str, ".short", strlen(".short"))==0)
192 Length += 2*countArguments(Str+strlen(".short"));
193 else if (strncasecmp(Str, ".byte", strlen(".byte"))==0)
194 Length += 1*countArguments(Str+strlen(".byte"));
195 else if (strncasecmp(Str, ".single", strlen(".single"))==0)
196 Length += 4*countArguments(Str+strlen(".single"));
197 else if (strncasecmp(Str, ".double", strlen(".double"))==0)
198 Length += 8*countArguments(Str+strlen(".double"));
199 else if (strncasecmp(Str, ".quad", strlen(".quad"))==0)
200 Length += 16*countArguments(Str+strlen(".quad"));
201 else if (strncasecmp(Str, ".ascii", strlen(".ascii"))==0)
202 Length += countString(Str+strlen(".ascii"));
203 else if (strncasecmp(Str, ".asciz", strlen(".asciz"))==0)
204 Length += countString(Str+strlen(".asciz"))+1;
205 }
206 } else if (inTextSection) {
110207 // An instruction
111208 atInsnStart = false;
112 if (isThumb) {
209 if (Subtarget->isThumb()) {
113210 // BL and BLX are 4 bytes, all others 2.
114 if ((*Str=='b' || *Str=='B') &&
115 (*(Str+1)=='l' || *(Str+1)=='L')) {
116 if (*(Str+2)=='x' || *(Str+2)=='X') {
117 const char* p = Str+3;
118 while (*p && isspace(*p))
119 p++;
120 if (*p == 'r' || *p=='R')
121 Length += 2; // BLX reg
122 else
123 Length += 4; // BLX non-reg
124 }
211 if (strncasecmp(Str, "blx", strlen("blx"))==0) {
212 const char* p = Str+3;
213 while (*p && isspace(*p))
214 p++;
215 if (*p == 'r' || *p=='R')
216 Length += 2; // BLX reg
125217 else
126 Length += 4; // BL
127 } else
218 Length += 4; // BLX non-reg
219 } else if (strncasecmp(Str, "bl", strlen("bl"))==0)
220 Length += 4; // BL
221 else
128222 Length += 2; // Thumb anything else
129223 }
130224 else
1414 #define ARMTARGETASMINFO_H
1515
1616 #include "llvm/Target/TargetAsmInfo.h"
17 #include "ARMSubtarget.h"
1718
1819 namespace llvm {
1920
2324 struct ARMTargetAsmInfo : public TargetAsmInfo {
2425 ARMTargetAsmInfo(const ARMTargetMachine &TM);
2526
26 bool isThumb;
27 const ARMSubtarget *Subtarget;
2728
2829 virtual unsigned getInlineAsmLength(const char *Str) const;
30 unsigned countArguments(const char *p) const;
31 unsigned countString(const char *p) const;
2932 };
3033
3134