blob: 5662fea86ced2f13e99413aaaeff9fc1c84d6c47 [file] [log] [blame]
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +00001//===- DarwinAsmParser.cpp - Darwin (Mach-O) Assembly Parser --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCParser/MCAsmParserExtension.h"
11#include "llvm/MC/MCContext.h"
12#include "llvm/MC/MCSectionMachO.h"
13#include "llvm/MC/MCStreamer.h"
14#include "llvm/MC/MCSymbol.h"
15#include "llvm/MC/MCParser/MCAsmLexer.h"
16#include "llvm/MC/MCParser/MCAsmParser.h"
Jim Grosbach3e965312012-05-18 19:12:01 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +000018#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/SourceMgr.h"
22using namespace llvm;
23
24namespace {
25
26/// \brief Implementation of directive handling which is shared across all
27/// Darwin targets.
28class DarwinAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000029 template<bool (DarwinAsmParser::*Handler)(StringRef, SMLoc)>
30 void AddDirectiveHandler(StringRef Directive) {
31 getParser().AddDirectiveHandler(this, Directive,
32 HandleDirective<DarwinAsmParser, Handler>);
33 }
34
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +000035 bool ParseSectionSwitch(const char *Segment, const char *Section,
36 unsigned TAA = 0, unsigned ImplicitAlign = 0,
37 unsigned StubSize = 0);
38
39public:
40 DarwinAsmParser() {}
41
42 virtual void Initialize(MCAsmParser &Parser) {
43 // Call the base implementation.
44 this->MCAsmParserExtension::Initialize(Parser);
45
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000046 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDesc>(".desc");
47 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveLsym>(".lsym");
48 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols>(
49 ".subsections_via_symbols");
50 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDumpOrLoad>(".dump");
51 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDumpOrLoad>(".load");
52 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveSection>(".section");
53 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveSecureLogUnique>(
54 ".secure_log_unique");
55 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveSecureLogReset>(
56 ".secure_log_reset");
57 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveTBSS>(".tbss");
58 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveZerofill>(".zerofill");
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +000059
Jim Grosbach3e965312012-05-18 19:12:01 +000060 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDataRegion>(".data_region");
61 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDataRegionEnd>(".end_data_region");
62
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +000063 // Special section directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000064 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConst>(".const");
65 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConstData>(".const_data");
66 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConstructor>(".constructor");
67 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveCString>(".cstring");
68 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveData>(".data");
69 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveDestructor>(".destructor");
70 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveDyld>(".dyld");
71 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveFVMLibInit0>(".fvmlib_init0");
72 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveFVMLibInit1>(".fvmlib_init1");
73 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLazySymbolPointers>(".lazy_symbol_pointer");
74 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral16>(".literal16");
75 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral4>(".literal4");
76 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral8>(".literal8");
77 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveModInitFunc>(".mod_init_func");
78 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveModTermFunc>(".mod_term_func");
79 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveNonLazySymbolPointers>(".non_lazy_symbol_pointer");
80 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCatClsMeth>(".objc_cat_cls_meth");
81 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCatInstMeth>(".objc_cat_inst_meth");
82 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCategory>(".objc_category");
83 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClass>(".objc_class");
84 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClassNames>(".objc_class_names");
85 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClassVars>(".objc_class_vars");
86 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClsMeth>(".objc_cls_meth");
87 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClsRefs>(".objc_cls_refs");
88 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCInstMeth>(".objc_inst_meth");
89 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCInstanceVars>(".objc_instance_vars");
90 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMessageRefs>(".objc_message_refs");
91 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMetaClass>(".objc_meta_class");
92 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMethVarNames>(".objc_meth_var_names");
93 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMethVarTypes>(".objc_meth_var_types");
94 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCModuleInfo>(".objc_module_info");
95 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCProtocol>(".objc_protocol");
96 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCSelectorStrs>(".objc_selector_strs");
97 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCStringObject>(".objc_string_object");
98 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCSymbols>(".objc_symbols");
99 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectivePICSymbolStub>(".picsymbol_stub");
100 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveStaticConst>(".static_const");
101 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveStaticData>(".static_data");
102 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveSymbolStub>(".symbol_stub");
103 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveTData>(".tdata");
104 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveText>(".text");
105 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveThreadInitFunc>(".thread_init_func");
106 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveTLV>(".tlv");
Jim Grosbach8270da82011-03-08 19:17:19 +0000107
108 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveIdent>(".ident");
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000109 }
110
111 bool ParseDirectiveDesc(StringRef, SMLoc);
112 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
113 bool ParseDirectiveLsym(StringRef, SMLoc);
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000114 bool ParseDirectiveSection(StringRef, SMLoc);
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000115 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
116 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
117 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
118 bool ParseDirectiveTBSS(StringRef, SMLoc);
119 bool ParseDirectiveZerofill(StringRef, SMLoc);
Jim Grosbach3e965312012-05-18 19:12:01 +0000120 bool ParseDirectiveDataRegion(StringRef, SMLoc);
121 bool ParseDirectiveDataRegionEnd(StringRef, SMLoc);
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000122
123 // Named Section Directive
124 bool ParseSectionDirectiveConst(StringRef, SMLoc) {
125 return ParseSectionSwitch("__TEXT", "__const");
126 }
127 bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) {
128 return ParseSectionSwitch("__TEXT", "__static_const");
129 }
130 bool ParseSectionDirectiveCString(StringRef, SMLoc) {
131 return ParseSectionSwitch("__TEXT","__cstring",
132 MCSectionMachO::S_CSTRING_LITERALS);
133 }
134 bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) {
135 return ParseSectionSwitch("__TEXT", "__literal4",
136 MCSectionMachO::S_4BYTE_LITERALS, 4);
137 }
138 bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) {
139 return ParseSectionSwitch("__TEXT", "__literal8",
140 MCSectionMachO::S_8BYTE_LITERALS, 8);
141 }
142 bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) {
143 return ParseSectionSwitch("__TEXT","__literal16",
144 MCSectionMachO::S_16BYTE_LITERALS, 16);
145 }
146 bool ParseSectionDirectiveConstructor(StringRef, SMLoc) {
147 return ParseSectionSwitch("__TEXT","__constructor");
148 }
149 bool ParseSectionDirectiveDestructor(StringRef, SMLoc) {
150 return ParseSectionSwitch("__TEXT","__destructor");
151 }
152 bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
153 return ParseSectionSwitch("__TEXT","__fvmlib_init0");
154 }
155 bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
156 return ParseSectionSwitch("__TEXT","__fvmlib_init1");
157 }
158 bool ParseSectionDirectiveSymbolStub(StringRef, SMLoc) {
159 return ParseSectionSwitch("__TEXT","__symbol_stub",
160 MCSectionMachO::S_SYMBOL_STUBS |
161 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
162 // FIXME: Different on PPC and ARM.
163 0, 16);
164 }
165 bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
166 return ParseSectionSwitch("__TEXT","__picsymbol_stub",
167 MCSectionMachO::S_SYMBOL_STUBS |
168 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
169 }
170 bool ParseSectionDirectiveData(StringRef, SMLoc) {
171 return ParseSectionSwitch("__DATA", "__data");
172 }
173 bool ParseSectionDirectiveStaticData(StringRef, SMLoc) {
174 return ParseSectionSwitch("__DATA", "__static_data");
175 }
176 bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
177 return ParseSectionSwitch("__DATA", "__nl_symbol_ptr",
178 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
179 }
180 bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
181 return ParseSectionSwitch("__DATA", "__la_symbol_ptr",
182 MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 4);
183 }
184 bool ParseSectionDirectiveDyld(StringRef, SMLoc) {
185 return ParseSectionSwitch("__DATA", "__dyld");
186 }
187 bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) {
188 return ParseSectionSwitch("__DATA", "__mod_init_func",
189 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 4);
190 }
191 bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) {
192 return ParseSectionSwitch("__DATA", "__mod_term_func",
193 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 4);
194 }
195 bool ParseSectionDirectiveConstData(StringRef, SMLoc) {
196 return ParseSectionSwitch("__DATA", "__const");
197 }
198 bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) {
199 return ParseSectionSwitch("__OBJC", "__class",
200 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
201 }
202 bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
203 return ParseSectionSwitch("__OBJC", "__meta_class",
204 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
205 }
206 bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
207 return ParseSectionSwitch("__OBJC", "__cat_cls_meth",
208 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
209 }
210 bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
211 return ParseSectionSwitch("__OBJC", "__cat_inst_meth",
212 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
213 }
214 bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
215 return ParseSectionSwitch("__OBJC", "__protocol",
216 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
217 }
218 bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
219 return ParseSectionSwitch("__OBJC", "__string_object",
220 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
221 }
222 bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
223 return ParseSectionSwitch("__OBJC", "__cls_meth",
224 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
225 }
226 bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
227 return ParseSectionSwitch("__OBJC", "__inst_meth",
228 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
229 }
230 bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
231 return ParseSectionSwitch("__OBJC", "__cls_refs",
232 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
233 MCSectionMachO::S_LITERAL_POINTERS, 4);
234 }
235 bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
236 return ParseSectionSwitch("__OBJC", "__message_refs",
237 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
238 MCSectionMachO::S_LITERAL_POINTERS, 4);
239 }
240 bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
241 return ParseSectionSwitch("__OBJC", "__symbols",
242 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
243 }
244 bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) {
245 return ParseSectionSwitch("__OBJC", "__category",
246 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
247 }
248 bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
249 return ParseSectionSwitch("__OBJC", "__class_vars",
250 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
251 }
252 bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
253 return ParseSectionSwitch("__OBJC", "__instance_vars",
254 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
255 }
256 bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
257 return ParseSectionSwitch("__OBJC", "__module_info",
258 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
259 }
260 bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
261 return ParseSectionSwitch("__TEXT", "__cstring",
262 MCSectionMachO::S_CSTRING_LITERALS);
263 }
264 bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
265 return ParseSectionSwitch("__TEXT", "__cstring",
266 MCSectionMachO::S_CSTRING_LITERALS);
267 }
268 bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
269 return ParseSectionSwitch("__TEXT", "__cstring",
270 MCSectionMachO::S_CSTRING_LITERALS);
271 }
272 bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
273 return ParseSectionSwitch("__OBJC", "__selector_strs",
274 MCSectionMachO::S_CSTRING_LITERALS);
275 }
276 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
277 return ParseSectionSwitch("__DATA", "__thread_data",
278 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
279 }
280 bool ParseSectionDirectiveText(StringRef, SMLoc) {
281 return ParseSectionSwitch("__TEXT", "__text",
282 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
283 }
284 bool ParseSectionDirectiveTLV(StringRef, SMLoc) {
285 return ParseSectionSwitch("__DATA", "__thread_vars",
286 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
287 }
Jim Grosbach8270da82011-03-08 19:17:19 +0000288 bool ParseSectionDirectiveIdent(StringRef, SMLoc) {
289 // Darwin silently ignores the .ident directive.
290 getParser().EatToEndOfStatement();
291 return false;
292 }
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000293 bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
294 return ParseSectionSwitch("__DATA", "__thread_init",
295 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
296 }
297
298};
299
300}
301
302bool DarwinAsmParser::ParseSectionSwitch(const char *Segment,
303 const char *Section,
304 unsigned TAA, unsigned Align,
305 unsigned StubSize) {
306 if (getLexer().isNot(AsmToken::EndOfStatement))
307 return TokError("unexpected token in section switching directive");
308 Lex();
309
310 // FIXME: Arch specific.
311 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
312 getStreamer().SwitchSection(getContext().getMachOSection(
313 Segment, Section, TAA, StubSize,
314 isText ? SectionKind::getText()
315 : SectionKind::getDataRel()));
316
317 // Set the implicit alignment, if any.
318 //
319 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
320 // alignment on the section (e.g., if one manually inserts bytes into the
Bill Wendlingcf2561d2010-10-19 10:18:23 +0000321 // section, then just issuing the section switch directive will not realign
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000322 // the section. However, this is arguably more reasonable behavior, and there
323 // is no good reason for someone to intentionally emit incorrectly sized
324 // values into the implicitly aligned sections.
325 if (Align)
326 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
327
328 return false;
329}
330
331/// ParseDirectiveDesc
332/// ::= .desc identifier , expression
333bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
334 StringRef Name;
335 if (getParser().ParseIdentifier(Name))
336 return TokError("expected identifier in directive");
337
338 // Handle the identifier as the key symbol.
339 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
340
341 if (getLexer().isNot(AsmToken::Comma))
342 return TokError("unexpected token in '.desc' directive");
343 Lex();
344
345 int64_t DescValue;
346 if (getParser().ParseAbsoluteExpression(DescValue))
347 return true;
348
349 if (getLexer().isNot(AsmToken::EndOfStatement))
350 return TokError("unexpected token in '.desc' directive");
351
352 Lex();
353
354 // Set the n_desc field of this Symbol to this DescValue
355 getStreamer().EmitSymbolDesc(Sym, DescValue);
356
357 return false;
358}
359
360/// ParseDirectiveDumpOrLoad
361/// ::= ( .dump | .load ) "filename"
362bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
363 SMLoc IDLoc) {
364 bool IsDump = Directive == ".dump";
365 if (getLexer().isNot(AsmToken::String))
366 return TokError("expected string in '.dump' or '.load' directive");
367
368 Lex();
369
370 if (getLexer().isNot(AsmToken::EndOfStatement))
371 return TokError("unexpected token in '.dump' or '.load' directive");
372
373 Lex();
374
375 // FIXME: If/when .dump and .load are implemented they will be done in the
376 // the assembly parser and not have any need for an MCStreamer API.
377 if (IsDump)
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000378 return Warning(IDLoc, "ignoring directive .dump for now");
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000379 else
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000380 return Warning(IDLoc, "ignoring directive .load for now");
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000381}
382
383/// ParseDirectiveLsym
384/// ::= .lsym identifier , expression
385bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
386 StringRef Name;
387 if (getParser().ParseIdentifier(Name))
388 return TokError("expected identifier in directive");
389
390 // Handle the identifier as the key symbol.
391 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
392
393 if (getLexer().isNot(AsmToken::Comma))
394 return TokError("unexpected token in '.lsym' directive");
395 Lex();
396
397 const MCExpr *Value;
398 if (getParser().ParseExpression(Value))
399 return true;
400
401 if (getLexer().isNot(AsmToken::EndOfStatement))
402 return TokError("unexpected token in '.lsym' directive");
403
404 Lex();
405
406 // We don't currently support this directive.
407 //
408 // FIXME: Diagnostic location!
409 (void) Sym;
410 return TokError("directive '.lsym' is unsupported");
411}
412
413/// ParseDirectiveSection:
414/// ::= .section identifier (',' identifier)*
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000415bool DarwinAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000416 SMLoc Loc = getLexer().getLoc();
417
418 StringRef SectionName;
419 if (getParser().ParseIdentifier(SectionName))
420 return Error(Loc, "expected identifier after '.section' directive");
421
422 // Verify there is a following comma.
423 if (!getLexer().is(AsmToken::Comma))
424 return TokError("unexpected token in '.section' directive");
425
426 std::string SectionSpec = SectionName;
427 SectionSpec += ",";
428
429 // Add all the tokens until the end of the line, ParseSectionSpecifier will
430 // handle this.
431 StringRef EOL = getLexer().LexUntilEndOfStatement();
432 SectionSpec.append(EOL.begin(), EOL.end());
433
434 Lex();
435 if (getLexer().isNot(AsmToken::EndOfStatement))
436 return TokError("unexpected token in '.section' directive");
437 Lex();
438
439
440 StringRef Segment, Section;
Daniel Dunbar8d06ffc2011-03-17 16:25:24 +0000441 unsigned StubSize;
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000442 unsigned TAA;
443 bool TAAParsed;
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000444 std::string ErrorStr =
445 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
Stuart Hastings65c8bca2011-03-19 02:42:31 +0000446 TAA, TAAParsed, StubSize);
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000447
448 if (!ErrorStr.empty())
449 return Error(Loc, ErrorStr.c_str());
450
451 // FIXME: Arch specific.
452 bool isText = Segment == "__TEXT"; // FIXME: Hack.
453 getStreamer().SwitchSection(getContext().getMachOSection(
454 Segment, Section, TAA, StubSize,
455 isText ? SectionKind::getText()
456 : SectionKind::getDataRel()));
457 return false;
458}
459
460/// ParseDirectiveSecureLogUnique
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000461/// ::= .secure_log_unique ... message ...
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000462bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000463 StringRef LogMessage = getParser().ParseStringToEndOfStatement();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000464 if (getLexer().isNot(AsmToken::EndOfStatement))
465 return TokError("unexpected token in '.secure_log_unique' directive");
466
467 if (getContext().getSecureLogUsed() != false)
468 return Error(IDLoc, ".secure_log_unique specified multiple times");
469
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000470 // Get the secure log path.
471 const char *SecureLogFile = getContext().getSecureLogFile();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000472 if (SecureLogFile == NULL)
473 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
474 "environment variable unset.");
475
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000476 // Open the secure log file if we haven't already.
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000477 raw_ostream *OS = getContext().getSecureLog();
478 if (OS == NULL) {
479 std::string Err;
480 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
481 if (!Err.empty()) {
482 delete OS;
483 return Error(IDLoc, Twine("can't open secure log file: ") +
484 SecureLogFile + " (" + Err + ")");
485 }
486 getContext().setSecureLog(OS);
487 }
488
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000489 // Write the message.
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000490 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
491 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
492 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
493 << LogMessage + "\n";
494
495 getContext().setSecureLogUsed(true);
496
497 return false;
498}
499
500/// ParseDirectiveSecureLogReset
501/// ::= .secure_log_reset
502bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
503 if (getLexer().isNot(AsmToken::EndOfStatement))
504 return TokError("unexpected token in '.secure_log_reset' directive");
505
506 Lex();
507
508 getContext().setSecureLogUsed(false);
509
510 return false;
511}
512
513/// ParseDirectiveSubsectionsViaSymbols
514/// ::= .subsections_via_symbols
515bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
516 if (getLexer().isNot(AsmToken::EndOfStatement))
517 return TokError("unexpected token in '.subsections_via_symbols' directive");
518
519 Lex();
520
521 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
522
523 return false;
524}
525
526/// ParseDirectiveTBSS
527/// ::= .tbss identifier, size, align
528bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
529 SMLoc IDLoc = getLexer().getLoc();
530 StringRef Name;
531 if (getParser().ParseIdentifier(Name))
532 return TokError("expected identifier in directive");
533
534 // Handle the identifier as the key symbol.
535 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
536
537 if (getLexer().isNot(AsmToken::Comma))
538 return TokError("unexpected token in directive");
539 Lex();
540
541 int64_t Size;
542 SMLoc SizeLoc = getLexer().getLoc();
543 if (getParser().ParseAbsoluteExpression(Size))
544 return true;
545
546 int64_t Pow2Alignment = 0;
547 SMLoc Pow2AlignmentLoc;
548 if (getLexer().is(AsmToken::Comma)) {
549 Lex();
550 Pow2AlignmentLoc = getLexer().getLoc();
551 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
552 return true;
553 }
554
555 if (getLexer().isNot(AsmToken::EndOfStatement))
556 return TokError("unexpected token in '.tbss' directive");
557
558 Lex();
559
560 if (Size < 0)
561 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
562 "zero");
563
564 // FIXME: Diagnose overflow.
565 if (Pow2Alignment < 0)
566 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
567 "than zero");
568
569 if (!Sym->isUndefined())
570 return Error(IDLoc, "invalid symbol redefinition");
571
572 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
573 "__DATA", "__thread_bss",
574 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
575 0, SectionKind::getThreadBSS()),
576 Sym, Size, 1 << Pow2Alignment);
577
578 return false;
579}
580
581/// ParseDirectiveZerofill
582/// ::= .zerofill segname , sectname [, identifier , size_expression [
583/// , align_expression ]]
584bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
585 StringRef Segment;
586 if (getParser().ParseIdentifier(Segment))
587 return TokError("expected segment name after '.zerofill' directive");
588
589 if (getLexer().isNot(AsmToken::Comma))
590 return TokError("unexpected token in directive");
591 Lex();
592
593 StringRef Section;
594 if (getParser().ParseIdentifier(Section))
595 return TokError("expected section name after comma in '.zerofill' "
596 "directive");
597
598 // If this is the end of the line all that was wanted was to create the
599 // the section but with no symbol.
600 if (getLexer().is(AsmToken::EndOfStatement)) {
601 // Create the zerofill section but no symbol
602 getStreamer().EmitZerofill(getContext().getMachOSection(
603 Segment, Section, MCSectionMachO::S_ZEROFILL,
604 0, SectionKind::getBSS()));
605 return false;
606 }
607
608 if (getLexer().isNot(AsmToken::Comma))
609 return TokError("unexpected token in directive");
610 Lex();
611
612 SMLoc IDLoc = getLexer().getLoc();
613 StringRef IDStr;
614 if (getParser().ParseIdentifier(IDStr))
615 return TokError("expected identifier in directive");
616
617 // handle the identifier as the key symbol.
618 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
619
620 if (getLexer().isNot(AsmToken::Comma))
621 return TokError("unexpected token in directive");
622 Lex();
623
624 int64_t Size;
625 SMLoc SizeLoc = getLexer().getLoc();
626 if (getParser().ParseAbsoluteExpression(Size))
627 return true;
628
629 int64_t Pow2Alignment = 0;
630 SMLoc Pow2AlignmentLoc;
631 if (getLexer().is(AsmToken::Comma)) {
632 Lex();
633 Pow2AlignmentLoc = getLexer().getLoc();
634 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
635 return true;
636 }
637
638 if (getLexer().isNot(AsmToken::EndOfStatement))
639 return TokError("unexpected token in '.zerofill' directive");
640
641 Lex();
642
643 if (Size < 0)
644 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
645 "than zero");
646
647 // NOTE: The alignment in the directive is a power of 2 value, the assembler
648 // may internally end up wanting an alignment in bytes.
649 // FIXME: Diagnose overflow.
650 if (Pow2Alignment < 0)
651 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
652 "can't be less than zero");
653
654 if (!Sym->isUndefined())
655 return Error(IDLoc, "invalid symbol redefinition");
656
657 // Create the zerofill Symbol with Size and Pow2Alignment
658 //
659 // FIXME: Arch specific.
660 getStreamer().EmitZerofill(getContext().getMachOSection(
661 Segment, Section, MCSectionMachO::S_ZEROFILL,
662 0, SectionKind::getBSS()),
663 Sym, Size, 1 << Pow2Alignment);
664
665 return false;
666}
667
Jim Grosbach3e965312012-05-18 19:12:01 +0000668/// ParseDirectiveDataRegion
669/// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
670bool DarwinAsmParser::ParseDirectiveDataRegion(StringRef, SMLoc) {
671 if (getLexer().is(AsmToken::EndOfStatement)) {
672 Lex();
673 getStreamer().EmitDataRegion(MCDR_DataRegion);
674 return false;
675 }
676 StringRef RegionType;
677 SMLoc Loc = getParser().getTok().getLoc();
678 if (getParser().ParseIdentifier(RegionType))
679 return TokError("expected region type after '.data_region' directive");
680 int Kind = StringSwitch<int>(RegionType)
681 .Case("jt8", MCDR_DataRegionJT8)
682 .Case("jt16", MCDR_DataRegionJT16)
683 .Case("jt32", MCDR_DataRegionJT32)
684 .Default(-1);
685 if (Kind == -1)
686 return Error(Loc, "unknown region type in '.data_region' directive");
687 Lex();
688
689 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
690 return false;
691}
692
693/// ParseDirectiveDataRegionEnd
694/// ::= .end_data_region
695bool DarwinAsmParser::ParseDirectiveDataRegionEnd(StringRef, SMLoc) {
696 if (getLexer().isNot(AsmToken::EndOfStatement))
697 return TokError("unexpected token in '.end_data_region' directive");
698
699 Lex();
700 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
701 return false;
702}
703
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000704namespace llvm {
705
706MCAsmParserExtension *createDarwinAsmParser() {
707 return new DarwinAsmParser;
708}
709
710}