1 //===- DarwinAsmParser.cpp - Darwin (Mach-O) Assembly Parser --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCObjectFileInfo.h"
17 #include "llvm/MC/MCParser/MCAsmLexer.h"
18 #include "llvm/MC/MCParser/MCAsmParser.h"
19 #include "llvm/MC/MCSectionMachO.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/SourceMgr.h"
29 /// \brief Implementation of directive handling which is shared across all
31 class DarwinAsmParser : public MCAsmParserExtension {
32 template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
33 void addDirectiveHandler(StringRef Directive) {
34 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
35 this, HandleDirective<DarwinAsmParser, HandlerMethod>);
36 getParser().addDirectiveHandler(Directive, Handler);
39 bool parseSectionSwitch(const char *Segment, const char *Section,
40 unsigned TAA = 0, unsigned ImplicitAlign = 0,
41 unsigned StubSize = 0);
43 SMLoc LastVersionMinDirective;
48 void Initialize(MCAsmParser &Parser) override {
49 // Call the base implementation.
50 this->MCAsmParserExtension::Initialize(Parser);
52 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(".desc");
53 addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
55 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLsym>(".lsym");
56 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSubsectionsViaSymbols>(
57 ".subsections_via_symbols");
58 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".dump");
59 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".load");
60 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSection>(".section");
61 addDirectiveHandler<&DarwinAsmParser::parseDirectivePushSection>(
63 addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
65 addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(".previous");
66 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
67 ".secure_log_unique");
68 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
70 addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(".tbss");
71 addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(".zerofill");
73 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
75 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
78 // Special section directives.
79 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(".bss");
80 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(".const");
81 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
83 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
85 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
87 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(".data");
88 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
90 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(".dyld");
91 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
93 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
96 &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
97 ".lazy_symbol_pointer");
98 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
100 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
102 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
104 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
106 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
108 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
111 &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
112 ".non_lazy_symbol_pointer");
113 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatClsMeth>(
114 ".objc_cat_cls_meth");
115 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatInstMeth>(
116 ".objc_cat_inst_meth");
117 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCategory>(
119 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
121 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
122 ".objc_class_names");
123 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
125 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
127 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
129 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
132 &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
133 ".objc_instance_vars");
134 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
135 ".objc_message_refs");
136 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
139 &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
140 ".objc_meth_var_names");
142 &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
143 ".objc_meth_var_types");
144 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
145 ".objc_module_info");
146 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
149 &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
150 ".objc_selector_strs");
152 &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
153 ".objc_string_object");
154 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
156 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
158 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
160 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
162 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
164 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(".tdata");
165 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(".text");
166 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
167 ".thread_init_func");
168 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv");
170 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveIdent>(".ident");
171 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
172 ".watchos_version_min");
173 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".tvos_version_min");
174 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".ios_version_min");
175 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
176 ".macosx_version_min");
178 LastVersionMinDirective = SMLoc();
181 bool parseDirectiveDesc(StringRef, SMLoc);
182 bool parseDirectiveIndirectSymbol(StringRef, SMLoc);
183 bool parseDirectiveDumpOrLoad(StringRef, SMLoc);
184 bool parseDirectiveLsym(StringRef, SMLoc);
185 bool parseDirectiveLinkerOption(StringRef, SMLoc);
186 bool parseDirectiveSection(StringRef, SMLoc);
187 bool parseDirectivePushSection(StringRef, SMLoc);
188 bool parseDirectivePopSection(StringRef, SMLoc);
189 bool parseDirectivePrevious(StringRef, SMLoc);
190 bool parseDirectiveSecureLogReset(StringRef, SMLoc);
191 bool parseDirectiveSecureLogUnique(StringRef, SMLoc);
192 bool parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
193 bool parseDirectiveTBSS(StringRef, SMLoc);
194 bool parseDirectiveZerofill(StringRef, SMLoc);
195 bool parseDirectiveDataRegion(StringRef, SMLoc);
196 bool parseDirectiveDataRegionEnd(StringRef, SMLoc);
198 // Named Section Directive
199 bool parseSectionDirectiveBss(StringRef, SMLoc) {
200 return parseSectionSwitch("__DATA", "__bss");
203 bool parseSectionDirectiveConst(StringRef, SMLoc) {
204 return parseSectionSwitch("__TEXT", "__const");
206 bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
207 return parseSectionSwitch("__TEXT", "__static_const");
209 bool parseSectionDirectiveCString(StringRef, SMLoc) {
210 return parseSectionSwitch("__TEXT","__cstring",
211 MachO::S_CSTRING_LITERALS);
213 bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
214 return parseSectionSwitch("__TEXT", "__literal4",
215 MachO::S_4BYTE_LITERALS, 4);
217 bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
218 return parseSectionSwitch("__TEXT", "__literal8",
219 MachO::S_8BYTE_LITERALS, 8);
221 bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
222 return parseSectionSwitch("__TEXT","__literal16",
223 MachO::S_16BYTE_LITERALS, 16);
225 bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
226 return parseSectionSwitch("__TEXT","__constructor");
228 bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
229 return parseSectionSwitch("__TEXT","__destructor");
231 bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
232 return parseSectionSwitch("__TEXT","__fvmlib_init0");
234 bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
235 return parseSectionSwitch("__TEXT","__fvmlib_init1");
237 bool parseSectionDirectiveSymbolStub(StringRef, SMLoc) {
238 return parseSectionSwitch("__TEXT","__symbol_stub",
239 MachO::S_SYMBOL_STUBS |
240 MachO::S_ATTR_PURE_INSTRUCTIONS,
241 // FIXME: Different on PPC and ARM.
244 bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
245 return parseSectionSwitch("__TEXT","__picsymbol_stub",
246 MachO::S_SYMBOL_STUBS |
247 MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
249 bool parseSectionDirectiveData(StringRef, SMLoc) {
250 return parseSectionSwitch("__DATA", "__data");
252 bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
253 return parseSectionSwitch("__DATA", "__static_data");
255 bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
256 return parseSectionSwitch("__DATA", "__nl_symbol_ptr",
257 MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
259 bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
260 return parseSectionSwitch("__DATA", "__la_symbol_ptr",
261 MachO::S_LAZY_SYMBOL_POINTERS, 4);
263 bool parseSectionDirectiveDyld(StringRef, SMLoc) {
264 return parseSectionSwitch("__DATA", "__dyld");
266 bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
267 return parseSectionSwitch("__DATA", "__mod_init_func",
268 MachO::S_MOD_INIT_FUNC_POINTERS, 4);
270 bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
271 return parseSectionSwitch("__DATA", "__mod_term_func",
272 MachO::S_MOD_TERM_FUNC_POINTERS, 4);
274 bool parseSectionDirectiveConstData(StringRef, SMLoc) {
275 return parseSectionSwitch("__DATA", "__const");
277 bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
278 return parseSectionSwitch("__OBJC", "__class",
279 MachO::S_ATTR_NO_DEAD_STRIP);
281 bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
282 return parseSectionSwitch("__OBJC", "__meta_class",
283 MachO::S_ATTR_NO_DEAD_STRIP);
285 bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
286 return parseSectionSwitch("__OBJC", "__cat_cls_meth",
287 MachO::S_ATTR_NO_DEAD_STRIP);
289 bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
290 return parseSectionSwitch("__OBJC", "__cat_inst_meth",
291 MachO::S_ATTR_NO_DEAD_STRIP);
293 bool parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
294 return parseSectionSwitch("__OBJC", "__protocol",
295 MachO::S_ATTR_NO_DEAD_STRIP);
297 bool parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
298 return parseSectionSwitch("__OBJC", "__string_object",
299 MachO::S_ATTR_NO_DEAD_STRIP);
301 bool parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
302 return parseSectionSwitch("__OBJC", "__cls_meth",
303 MachO::S_ATTR_NO_DEAD_STRIP);
305 bool parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
306 return parseSectionSwitch("__OBJC", "__inst_meth",
307 MachO::S_ATTR_NO_DEAD_STRIP);
309 bool parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
310 return parseSectionSwitch("__OBJC", "__cls_refs",
311 MachO::S_ATTR_NO_DEAD_STRIP |
312 MachO::S_LITERAL_POINTERS, 4);
314 bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
315 return parseSectionSwitch("__OBJC", "__message_refs",
316 MachO::S_ATTR_NO_DEAD_STRIP |
317 MachO::S_LITERAL_POINTERS, 4);
319 bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
320 return parseSectionSwitch("__OBJC", "__symbols",
321 MachO::S_ATTR_NO_DEAD_STRIP);
323 bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
324 return parseSectionSwitch("__OBJC", "__category",
325 MachO::S_ATTR_NO_DEAD_STRIP);
327 bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
328 return parseSectionSwitch("__OBJC", "__class_vars",
329 MachO::S_ATTR_NO_DEAD_STRIP);
331 bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
332 return parseSectionSwitch("__OBJC", "__instance_vars",
333 MachO::S_ATTR_NO_DEAD_STRIP);
335 bool parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
336 return parseSectionSwitch("__OBJC", "__module_info",
337 MachO::S_ATTR_NO_DEAD_STRIP);
339 bool parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
340 return parseSectionSwitch("__TEXT", "__cstring",
341 MachO::S_CSTRING_LITERALS);
343 bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
344 return parseSectionSwitch("__TEXT", "__cstring",
345 MachO::S_CSTRING_LITERALS);
347 bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
348 return parseSectionSwitch("__TEXT", "__cstring",
349 MachO::S_CSTRING_LITERALS);
351 bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
352 return parseSectionSwitch("__OBJC", "__selector_strs",
353 MachO::S_CSTRING_LITERALS);
355 bool parseSectionDirectiveTData(StringRef, SMLoc) {
356 return parseSectionSwitch("__DATA", "__thread_data",
357 MachO::S_THREAD_LOCAL_REGULAR);
359 bool parseSectionDirectiveText(StringRef, SMLoc) {
360 return parseSectionSwitch("__TEXT", "__text",
361 MachO::S_ATTR_PURE_INSTRUCTIONS);
363 bool parseSectionDirectiveTLV(StringRef, SMLoc) {
364 return parseSectionSwitch("__DATA", "__thread_vars",
365 MachO::S_THREAD_LOCAL_VARIABLES);
367 bool parseSectionDirectiveIdent(StringRef, SMLoc) {
368 // Darwin silently ignores the .ident directive.
369 getParser().eatToEndOfStatement();
372 bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
373 return parseSectionSwitch("__DATA", "__thread_init",
374 MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
376 bool parseVersionMin(StringRef, SMLoc);
380 } // end anonymous namespace
382 bool DarwinAsmParser::parseSectionSwitch(const char *Segment,
384 unsigned TAA, unsigned Align,
386 if (getLexer().isNot(AsmToken::EndOfStatement))
387 return TokError("unexpected token in section switching directive");
390 // FIXME: Arch specific.
391 bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
392 getStreamer().SwitchSection(getContext().getMachOSection(
393 Segment, Section, TAA, StubSize,
394 isText ? SectionKind::getText()
395 : SectionKind::getDataRel()));
397 // Set the implicit alignment, if any.
399 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
400 // alignment on the section (e.g., if one manually inserts bytes into the
401 // section, then just issuing the section switch directive will not realign
402 // the section. However, this is arguably more reasonable behavior, and there
403 // is no good reason for someone to intentionally emit incorrectly sized
404 // values into the implicitly aligned sections.
406 getStreamer().EmitValueToAlignment(Align);
411 /// parseDirectiveDesc
412 /// ::= .desc identifier , expression
413 bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
415 if (getParser().parseIdentifier(Name))
416 return TokError("expected identifier in directive");
418 // Handle the identifier as the key symbol.
419 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
421 if (getLexer().isNot(AsmToken::Comma))
422 return TokError("unexpected token in '.desc' directive");
426 if (getParser().parseAbsoluteExpression(DescValue))
429 if (getLexer().isNot(AsmToken::EndOfStatement))
430 return TokError("unexpected token in '.desc' directive");
434 // Set the n_desc field of this Symbol to this DescValue
435 getStreamer().EmitSymbolDesc(Sym, DescValue);
440 /// parseDirectiveIndirectSymbol
441 /// ::= .indirect_symbol identifier
442 bool DarwinAsmParser::parseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
443 const MCSectionMachO *Current = static_cast<const MCSectionMachO*>(
444 getStreamer().getCurrentSection().first);
445 MachO::SectionType SectionType = Current->getType();
446 if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
447 SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
448 SectionType != MachO::S_SYMBOL_STUBS)
449 return Error(Loc, "indirect symbol not in a symbol pointer or stub "
453 if (getParser().parseIdentifier(Name))
454 return TokError("expected identifier in .indirect_symbol directive");
456 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
458 // Assembler local symbols don't make any sense here. Complain loudly.
459 if (Sym->isTemporary())
460 return TokError("non-local symbol required in directive");
462 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
463 return TokError("unable to emit indirect symbol attribute for: " + Name);
465 if (getLexer().isNot(AsmToken::EndOfStatement))
466 return TokError("unexpected token in '.indirect_symbol' directive");
473 /// parseDirectiveDumpOrLoad
474 /// ::= ( .dump | .load ) "filename"
475 bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
477 bool IsDump = Directive == ".dump";
478 if (getLexer().isNot(AsmToken::String))
479 return TokError("expected string in '.dump' or '.load' directive");
483 if (getLexer().isNot(AsmToken::EndOfStatement))
484 return TokError("unexpected token in '.dump' or '.load' directive");
488 // FIXME: If/when .dump and .load are implemented they will be done in the
489 // the assembly parser and not have any need for an MCStreamer API.
491 return Warning(IDLoc, "ignoring directive .dump for now");
493 return Warning(IDLoc, "ignoring directive .load for now");
496 /// ParseDirectiveLinkerOption
497 /// ::= .linker_option "string" ( , "string" )*
498 bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
499 SmallVector<std::string, 4> Args;
501 if (getLexer().isNot(AsmToken::String))
502 return TokError("expected string in '" + Twine(IDVal) + "' directive");
505 if (getParser().parseEscapedString(Data))
508 Args.push_back(Data);
511 if (getLexer().is(AsmToken::EndOfStatement))
514 if (getLexer().isNot(AsmToken::Comma))
515 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
519 getStreamer().EmitLinkerOptions(Args);
523 /// parseDirectiveLsym
524 /// ::= .lsym identifier , expression
525 bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
527 if (getParser().parseIdentifier(Name))
528 return TokError("expected identifier in directive");
530 // Handle the identifier as the key symbol.
531 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
533 if (getLexer().isNot(AsmToken::Comma))
534 return TokError("unexpected token in '.lsym' directive");
538 if (getParser().parseExpression(Value))
541 if (getLexer().isNot(AsmToken::EndOfStatement))
542 return TokError("unexpected token in '.lsym' directive");
546 // We don't currently support this directive.
548 // FIXME: Diagnostic location!
550 return TokError("directive '.lsym' is unsupported");
553 /// parseDirectiveSection:
554 /// ::= .section identifier (',' identifier)*
555 bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
556 SMLoc Loc = getLexer().getLoc();
558 StringRef SectionName;
559 if (getParser().parseIdentifier(SectionName))
560 return Error(Loc, "expected identifier after '.section' directive");
562 // Verify there is a following comma.
563 if (!getLexer().is(AsmToken::Comma))
564 return TokError("unexpected token in '.section' directive");
566 std::string SectionSpec = SectionName;
569 // Add all the tokens until the end of the line, ParseSectionSpecifier will
571 StringRef EOL = getLexer().LexUntilEndOfStatement();
572 SectionSpec.append(EOL.begin(), EOL.end());
575 if (getLexer().isNot(AsmToken::EndOfStatement))
576 return TokError("unexpected token in '.section' directive");
580 StringRef Segment, Section;
584 std::string ErrorStr =
585 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
586 TAA, TAAParsed, StubSize);
588 if (!ErrorStr.empty())
589 return Error(Loc, ErrorStr.c_str());
591 // Issue a warning if the target is not powerpc and Section is a *coal* section.
592 Triple TT = getParser().getContext().getObjectFileInfo()->getTargetTriple();
593 Triple::ArchType ArchTy = TT.getArch();
595 if (ArchTy != Triple::ppc && ArchTy != Triple::ppc64) {
596 StringRef NonCoalSection = StringSwitch<StringRef>(Section)
597 .Case("__textcoal_nt", "__text")
598 .Case("__const_coal", "__const")
599 .Case("__datacoal_nt", "__data")
602 if (!Section.equals(NonCoalSection)) {
603 StringRef SectionVal(Loc.getPointer());
604 size_t B = SectionVal.find(',') + 1, E = SectionVal.find(',', B);
605 SMLoc BLoc = SMLoc::getFromPointer(SectionVal.data() + B);
606 SMLoc ELoc = SMLoc::getFromPointer(SectionVal.data() + E);
607 getParser().Warning(Loc, "section \"" + Section + "\" is deprecated",
608 SMRange(BLoc, ELoc));
609 getParser().Note(Loc, "change section name to \"" + NonCoalSection +
610 "\"", SMRange(BLoc, ELoc));
614 // FIXME: Arch specific.
615 bool isText = Segment == "__TEXT"; // FIXME: Hack.
616 getStreamer().SwitchSection(getContext().getMachOSection(
617 Segment, Section, TAA, StubSize,
618 isText ? SectionKind::getText()
619 : SectionKind::getDataRel()));
623 /// ParseDirectivePushSection:
624 /// ::= .pushsection identifier (',' identifier)*
625 bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
626 getStreamer().PushSection();
628 if (parseDirectiveSection(S, Loc)) {
629 getStreamer().PopSection();
636 /// ParseDirectivePopSection:
638 bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
639 if (!getStreamer().PopSection())
640 return TokError(".popsection without corresponding .pushsection");
644 /// ParseDirectivePrevious:
646 bool DarwinAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
647 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
648 if (!PreviousSection.first)
649 return TokError(".previous without corresponding .section");
650 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
654 /// ParseDirectiveSecureLogUnique
655 /// ::= .secure_log_unique ... message ...
656 bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
657 StringRef LogMessage = getParser().parseStringToEndOfStatement();
658 if (getLexer().isNot(AsmToken::EndOfStatement))
659 return TokError("unexpected token in '.secure_log_unique' directive");
661 if (getContext().getSecureLogUsed())
662 return Error(IDLoc, ".secure_log_unique specified multiple times");
664 // Get the secure log path.
665 const char *SecureLogFile = getContext().getSecureLogFile();
667 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
668 "environment variable unset.");
670 // Open the secure log file if we haven't already.
671 raw_ostream *OS = getContext().getSecureLog();
674 OS = new raw_fd_ostream(SecureLogFile, EC,
675 sys::fs::F_Append | sys::fs::F_Text);
678 return Error(IDLoc, Twine("can't open secure log file: ") +
679 SecureLogFile + " (" + EC.message() + ")");
681 getContext().setSecureLog(OS);
684 // Write the message.
685 unsigned CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
686 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
687 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
688 << LogMessage + "\n";
690 getContext().setSecureLogUsed(true);
695 /// ParseDirectiveSecureLogReset
696 /// ::= .secure_log_reset
697 bool DarwinAsmParser::parseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
698 if (getLexer().isNot(AsmToken::EndOfStatement))
699 return TokError("unexpected token in '.secure_log_reset' directive");
703 getContext().setSecureLogUsed(false);
708 /// parseDirectiveSubsectionsViaSymbols
709 /// ::= .subsections_via_symbols
710 bool DarwinAsmParser::parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
711 if (getLexer().isNot(AsmToken::EndOfStatement))
712 return TokError("unexpected token in '.subsections_via_symbols' directive");
716 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
721 /// ParseDirectiveTBSS
722 /// ::= .tbss identifier, size, align
723 bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
724 SMLoc IDLoc = getLexer().getLoc();
726 if (getParser().parseIdentifier(Name))
727 return TokError("expected identifier in directive");
729 // Handle the identifier as the key symbol.
730 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
732 if (getLexer().isNot(AsmToken::Comma))
733 return TokError("unexpected token in directive");
737 SMLoc SizeLoc = getLexer().getLoc();
738 if (getParser().parseAbsoluteExpression(Size))
741 int64_t Pow2Alignment = 0;
742 SMLoc Pow2AlignmentLoc;
743 if (getLexer().is(AsmToken::Comma)) {
745 Pow2AlignmentLoc = getLexer().getLoc();
746 if (getParser().parseAbsoluteExpression(Pow2Alignment))
750 if (getLexer().isNot(AsmToken::EndOfStatement))
751 return TokError("unexpected token in '.tbss' directive");
756 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
759 // FIXME: Diagnose overflow.
760 if (Pow2Alignment < 0)
761 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
764 if (!Sym->isUndefined())
765 return Error(IDLoc, "invalid symbol redefinition");
767 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
768 "__DATA", "__thread_bss",
769 MachO::S_THREAD_LOCAL_ZEROFILL,
770 0, SectionKind::getThreadBSS()),
771 Sym, Size, 1 << Pow2Alignment);
776 /// ParseDirectiveZerofill
777 /// ::= .zerofill segname , sectname [, identifier , size_expression [
778 /// , align_expression ]]
779 bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
781 if (getParser().parseIdentifier(Segment))
782 return TokError("expected segment name after '.zerofill' directive");
784 if (getLexer().isNot(AsmToken::Comma))
785 return TokError("unexpected token in directive");
789 if (getParser().parseIdentifier(Section))
790 return TokError("expected section name after comma in '.zerofill' "
793 // If this is the end of the line all that was wanted was to create the
794 // the section but with no symbol.
795 if (getLexer().is(AsmToken::EndOfStatement)) {
796 // Create the zerofill section but no symbol
797 getStreamer().EmitZerofill(getContext().getMachOSection(
798 Segment, Section, MachO::S_ZEROFILL,
799 0, SectionKind::getBSS()));
803 if (getLexer().isNot(AsmToken::Comma))
804 return TokError("unexpected token in directive");
807 SMLoc IDLoc = getLexer().getLoc();
809 if (getParser().parseIdentifier(IDStr))
810 return TokError("expected identifier in directive");
812 // handle the identifier as the key symbol.
813 MCSymbol *Sym = getContext().getOrCreateSymbol(IDStr);
815 if (getLexer().isNot(AsmToken::Comma))
816 return TokError("unexpected token in directive");
820 SMLoc SizeLoc = getLexer().getLoc();
821 if (getParser().parseAbsoluteExpression(Size))
824 int64_t Pow2Alignment = 0;
825 SMLoc Pow2AlignmentLoc;
826 if (getLexer().is(AsmToken::Comma)) {
828 Pow2AlignmentLoc = getLexer().getLoc();
829 if (getParser().parseAbsoluteExpression(Pow2Alignment))
833 if (getLexer().isNot(AsmToken::EndOfStatement))
834 return TokError("unexpected token in '.zerofill' directive");
839 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
842 // NOTE: The alignment in the directive is a power of 2 value, the assembler
843 // may internally end up wanting an alignment in bytes.
844 // FIXME: Diagnose overflow.
845 if (Pow2Alignment < 0)
846 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
847 "can't be less than zero");
849 if (!Sym->isUndefined())
850 return Error(IDLoc, "invalid symbol redefinition");
852 // Create the zerofill Symbol with Size and Pow2Alignment
854 // FIXME: Arch specific.
855 getStreamer().EmitZerofill(getContext().getMachOSection(
856 Segment, Section, MachO::S_ZEROFILL,
857 0, SectionKind::getBSS()),
858 Sym, Size, 1 << Pow2Alignment);
863 /// ParseDirectiveDataRegion
864 /// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
865 bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
866 if (getLexer().is(AsmToken::EndOfStatement)) {
868 getStreamer().EmitDataRegion(MCDR_DataRegion);
871 StringRef RegionType;
872 SMLoc Loc = getParser().getTok().getLoc();
873 if (getParser().parseIdentifier(RegionType))
874 return TokError("expected region type after '.data_region' directive");
875 int Kind = StringSwitch<int>(RegionType)
876 .Case("jt8", MCDR_DataRegionJT8)
877 .Case("jt16", MCDR_DataRegionJT16)
878 .Case("jt32", MCDR_DataRegionJT32)
881 return Error(Loc, "unknown region type in '.data_region' directive");
884 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
888 /// ParseDirectiveDataRegionEnd
889 /// ::= .end_data_region
890 bool DarwinAsmParser::parseDirectiveDataRegionEnd(StringRef, SMLoc) {
891 if (getLexer().isNot(AsmToken::EndOfStatement))
892 return TokError("unexpected token in '.end_data_region' directive");
895 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
900 /// ::= .ios_version_min major,minor[,update]
901 /// ::= .macosx_version_min major,minor[,update]
902 bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc Loc) {
903 int64_t Major = 0, Minor = 0, Update = 0;
904 int Kind = StringSwitch<int>(Directive)
905 .Case(".watchos_version_min", MCVM_WatchOSVersionMin)
906 .Case(".tvos_version_min", MCVM_TvOSVersionMin)
907 .Case(".ios_version_min", MCVM_IOSVersionMin)
908 .Case(".macosx_version_min", MCVM_OSXVersionMin);
909 // Get the major version number.
910 if (getLexer().isNot(AsmToken::Integer))
911 return TokError("invalid OS major version number");
912 Major = getLexer().getTok().getIntVal();
913 if (Major > 65535 || Major <= 0)
914 return TokError("invalid OS major version number");
916 if (getLexer().isNot(AsmToken::Comma))
917 return TokError("minor OS version number required, comma expected");
919 // Get the minor version number.
920 if (getLexer().isNot(AsmToken::Integer))
921 return TokError("invalid OS minor version number");
922 Minor = getLexer().getTok().getIntVal();
923 if (Minor > 255 || Minor < 0)
924 return TokError("invalid OS minor version number");
926 // Get the update level, if specified
927 if (getLexer().isNot(AsmToken::EndOfStatement)) {
928 if (getLexer().isNot(AsmToken::Comma))
929 return TokError("invalid update specifier, comma expected");
931 if (getLexer().isNot(AsmToken::Integer))
932 return TokError("invalid OS update number");
933 Update = getLexer().getTok().getIntVal();
934 if (Update > 255 || Update < 0)
935 return TokError("invalid OS update number");
939 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
940 Triple::OSType ExpectedOS = Triple::UnknownOS;
941 switch ((MCVersionMinType)Kind) {
942 case MCVM_WatchOSVersionMin: ExpectedOS = Triple::WatchOS; break;
943 case MCVM_TvOSVersionMin: ExpectedOS = Triple::TvOS; break;
944 case MCVM_IOSVersionMin: ExpectedOS = Triple::IOS; break;
945 case MCVM_OSXVersionMin: ExpectedOS = Triple::MacOSX; break;
947 if (T.getOS() != ExpectedOS)
948 Warning(Loc, Directive + " should only be used for " +
949 Triple::getOSTypeName(ExpectedOS) + " targets");
951 if (LastVersionMinDirective.isValid()) {
952 Warning(Loc, "overriding previous version_min directive");
953 Note(LastVersionMinDirective, "previous definition is here");
955 LastVersionMinDirective = Loc;
957 // We've parsed a correct version specifier, so send it to the streamer.
958 getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
965 MCAsmParserExtension *createDarwinAsmParser() {
966 return new DarwinAsmParser;
969 } // end llvm namespace