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/STLExtras.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCObjectFileInfo.h"
18 #include "llvm/MC/MCParser/MCAsmLexer.h"
19 #include "llvm/MC/MCParser/MCAsmParser.h"
20 #include "llvm/MC/MCSectionMachO.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/SourceMgr.h"
30 /// \brief Implementation of directive handling which is shared across all
32 class DarwinAsmParser : public MCAsmParserExtension {
33 template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
34 void addDirectiveHandler(StringRef Directive) {
35 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
36 this, HandleDirective<DarwinAsmParser, HandlerMethod>);
37 getParser().addDirectiveHandler(Directive, Handler);
40 bool parseSectionSwitch(const char *Segment, const char *Section,
41 unsigned TAA = 0, unsigned ImplicitAlign = 0,
42 unsigned StubSize = 0);
44 SMLoc LastVersionMinDirective;
49 void Initialize(MCAsmParser &Parser) override {
50 // Call the base implementation.
51 this->MCAsmParserExtension::Initialize(Parser);
53 addDirectiveHandler<&DarwinAsmParser::parseDirectiveAltEntry>(".alt_entry");
54 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(".desc");
55 addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
57 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLsym>(".lsym");
58 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSubsectionsViaSymbols>(
59 ".subsections_via_symbols");
60 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".dump");
61 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".load");
62 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSection>(".section");
63 addDirectiveHandler<&DarwinAsmParser::parseDirectivePushSection>(
65 addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
67 addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(".previous");
68 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
69 ".secure_log_unique");
70 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
72 addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(".tbss");
73 addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(".zerofill");
75 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
77 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
80 // Special section directives.
81 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(".bss");
82 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(".const");
83 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
85 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
87 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
89 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(".data");
90 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
92 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(".dyld");
93 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
95 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
98 &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
99 ".lazy_symbol_pointer");
100 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
102 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
104 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
106 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
108 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
110 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
113 &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
114 ".non_lazy_symbol_pointer");
116 &DarwinAsmParser::parseSectionDirectiveThreadLocalVariablePointers>(
117 ".thread_local_variable_pointer");
118 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatClsMeth>(
119 ".objc_cat_cls_meth");
120 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatInstMeth>(
121 ".objc_cat_inst_meth");
122 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCategory>(
124 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
126 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
127 ".objc_class_names");
128 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
130 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
132 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
134 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
137 &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
138 ".objc_instance_vars");
139 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
140 ".objc_message_refs");
141 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
144 &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
145 ".objc_meth_var_names");
147 &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
148 ".objc_meth_var_types");
149 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
150 ".objc_module_info");
151 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
154 &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
155 ".objc_selector_strs");
157 &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
158 ".objc_string_object");
159 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
161 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
163 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
165 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
167 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
169 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(".tdata");
170 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(".text");
171 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
172 ".thread_init_func");
173 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv");
175 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveIdent>(".ident");
176 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
177 ".watchos_version_min");
178 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".tvos_version_min");
179 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".ios_version_min");
180 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
181 ".macosx_version_min");
183 LastVersionMinDirective = SMLoc();
186 bool parseDirectiveAltEntry(StringRef, SMLoc);
187 bool parseDirectiveDesc(StringRef, SMLoc);
188 bool parseDirectiveIndirectSymbol(StringRef, SMLoc);
189 bool parseDirectiveDumpOrLoad(StringRef, SMLoc);
190 bool parseDirectiveLsym(StringRef, SMLoc);
191 bool parseDirectiveLinkerOption(StringRef, SMLoc);
192 bool parseDirectiveSection(StringRef, SMLoc);
193 bool parseDirectivePushSection(StringRef, SMLoc);
194 bool parseDirectivePopSection(StringRef, SMLoc);
195 bool parseDirectivePrevious(StringRef, SMLoc);
196 bool parseDirectiveSecureLogReset(StringRef, SMLoc);
197 bool parseDirectiveSecureLogUnique(StringRef, SMLoc);
198 bool parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
199 bool parseDirectiveTBSS(StringRef, SMLoc);
200 bool parseDirectiveZerofill(StringRef, SMLoc);
201 bool parseDirectiveDataRegion(StringRef, SMLoc);
202 bool parseDirectiveDataRegionEnd(StringRef, SMLoc);
204 // Named Section Directive
205 bool parseSectionDirectiveBss(StringRef, SMLoc) {
206 return parseSectionSwitch("__DATA", "__bss");
209 bool parseSectionDirectiveConst(StringRef, SMLoc) {
210 return parseSectionSwitch("__TEXT", "__const");
212 bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
213 return parseSectionSwitch("__TEXT", "__static_const");
215 bool parseSectionDirectiveCString(StringRef, SMLoc) {
216 return parseSectionSwitch("__TEXT","__cstring",
217 MachO::S_CSTRING_LITERALS);
219 bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
220 return parseSectionSwitch("__TEXT", "__literal4",
221 MachO::S_4BYTE_LITERALS, 4);
223 bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
224 return parseSectionSwitch("__TEXT", "__literal8",
225 MachO::S_8BYTE_LITERALS, 8);
227 bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
228 return parseSectionSwitch("__TEXT","__literal16",
229 MachO::S_16BYTE_LITERALS, 16);
231 bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
232 return parseSectionSwitch("__TEXT","__constructor");
234 bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
235 return parseSectionSwitch("__TEXT","__destructor");
237 bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
238 return parseSectionSwitch("__TEXT","__fvmlib_init0");
240 bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
241 return parseSectionSwitch("__TEXT","__fvmlib_init1");
243 bool parseSectionDirectiveSymbolStub(StringRef, SMLoc) {
244 return parseSectionSwitch("__TEXT","__symbol_stub",
245 MachO::S_SYMBOL_STUBS |
246 MachO::S_ATTR_PURE_INSTRUCTIONS,
247 // FIXME: Different on PPC and ARM.
250 bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
251 return parseSectionSwitch("__TEXT","__picsymbol_stub",
252 MachO::S_SYMBOL_STUBS |
253 MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
255 bool parseSectionDirectiveData(StringRef, SMLoc) {
256 return parseSectionSwitch("__DATA", "__data");
258 bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
259 return parseSectionSwitch("__DATA", "__static_data");
261 bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
262 return parseSectionSwitch("__DATA", "__nl_symbol_ptr",
263 MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
265 bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
266 return parseSectionSwitch("__DATA", "__la_symbol_ptr",
267 MachO::S_LAZY_SYMBOL_POINTERS, 4);
269 bool parseSectionDirectiveThreadLocalVariablePointers(StringRef, SMLoc) {
270 return parseSectionSwitch("__DATA", "__thread_ptr",
271 MachO::S_THREAD_LOCAL_VARIABLE_POINTERS, 4);
273 bool parseSectionDirectiveDyld(StringRef, SMLoc) {
274 return parseSectionSwitch("__DATA", "__dyld");
276 bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
277 return parseSectionSwitch("__DATA", "__mod_init_func",
278 MachO::S_MOD_INIT_FUNC_POINTERS, 4);
280 bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
281 return parseSectionSwitch("__DATA", "__mod_term_func",
282 MachO::S_MOD_TERM_FUNC_POINTERS, 4);
284 bool parseSectionDirectiveConstData(StringRef, SMLoc) {
285 return parseSectionSwitch("__DATA", "__const");
287 bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
288 return parseSectionSwitch("__OBJC", "__class",
289 MachO::S_ATTR_NO_DEAD_STRIP);
291 bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
292 return parseSectionSwitch("__OBJC", "__meta_class",
293 MachO::S_ATTR_NO_DEAD_STRIP);
295 bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
296 return parseSectionSwitch("__OBJC", "__cat_cls_meth",
297 MachO::S_ATTR_NO_DEAD_STRIP);
299 bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
300 return parseSectionSwitch("__OBJC", "__cat_inst_meth",
301 MachO::S_ATTR_NO_DEAD_STRIP);
303 bool parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
304 return parseSectionSwitch("__OBJC", "__protocol",
305 MachO::S_ATTR_NO_DEAD_STRIP);
307 bool parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
308 return parseSectionSwitch("__OBJC", "__string_object",
309 MachO::S_ATTR_NO_DEAD_STRIP);
311 bool parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
312 return parseSectionSwitch("__OBJC", "__cls_meth",
313 MachO::S_ATTR_NO_DEAD_STRIP);
315 bool parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
316 return parseSectionSwitch("__OBJC", "__inst_meth",
317 MachO::S_ATTR_NO_DEAD_STRIP);
319 bool parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
320 return parseSectionSwitch("__OBJC", "__cls_refs",
321 MachO::S_ATTR_NO_DEAD_STRIP |
322 MachO::S_LITERAL_POINTERS, 4);
324 bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
325 return parseSectionSwitch("__OBJC", "__message_refs",
326 MachO::S_ATTR_NO_DEAD_STRIP |
327 MachO::S_LITERAL_POINTERS, 4);
329 bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
330 return parseSectionSwitch("__OBJC", "__symbols",
331 MachO::S_ATTR_NO_DEAD_STRIP);
333 bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
334 return parseSectionSwitch("__OBJC", "__category",
335 MachO::S_ATTR_NO_DEAD_STRIP);
337 bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
338 return parseSectionSwitch("__OBJC", "__class_vars",
339 MachO::S_ATTR_NO_DEAD_STRIP);
341 bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
342 return parseSectionSwitch("__OBJC", "__instance_vars",
343 MachO::S_ATTR_NO_DEAD_STRIP);
345 bool parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
346 return parseSectionSwitch("__OBJC", "__module_info",
347 MachO::S_ATTR_NO_DEAD_STRIP);
349 bool parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
350 return parseSectionSwitch("__TEXT", "__cstring",
351 MachO::S_CSTRING_LITERALS);
353 bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
354 return parseSectionSwitch("__TEXT", "__cstring",
355 MachO::S_CSTRING_LITERALS);
357 bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
358 return parseSectionSwitch("__TEXT", "__cstring",
359 MachO::S_CSTRING_LITERALS);
361 bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
362 return parseSectionSwitch("__OBJC", "__selector_strs",
363 MachO::S_CSTRING_LITERALS);
365 bool parseSectionDirectiveTData(StringRef, SMLoc) {
366 return parseSectionSwitch("__DATA", "__thread_data",
367 MachO::S_THREAD_LOCAL_REGULAR);
369 bool parseSectionDirectiveText(StringRef, SMLoc) {
370 return parseSectionSwitch("__TEXT", "__text",
371 MachO::S_ATTR_PURE_INSTRUCTIONS);
373 bool parseSectionDirectiveTLV(StringRef, SMLoc) {
374 return parseSectionSwitch("__DATA", "__thread_vars",
375 MachO::S_THREAD_LOCAL_VARIABLES);
377 bool parseSectionDirectiveIdent(StringRef, SMLoc) {
378 // Darwin silently ignores the .ident directive.
379 getParser().eatToEndOfStatement();
382 bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
383 return parseSectionSwitch("__DATA", "__thread_init",
384 MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
386 bool parseVersionMin(StringRef, SMLoc);
390 } // end anonymous namespace
392 bool DarwinAsmParser::parseSectionSwitch(const char *Segment,
394 unsigned TAA, unsigned Align,
396 if (getLexer().isNot(AsmToken::EndOfStatement))
397 return TokError("unexpected token in section switching directive");
400 // FIXME: Arch specific.
401 bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
402 getStreamer().SwitchSection(getContext().getMachOSection(
403 Segment, Section, TAA, StubSize,
404 isText ? SectionKind::getText() : SectionKind::getData()));
406 // Set the implicit alignment, if any.
408 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
409 // alignment on the section (e.g., if one manually inserts bytes into the
410 // section, then just issuing the section switch directive will not realign
411 // the section. However, this is arguably more reasonable behavior, and there
412 // is no good reason for someone to intentionally emit incorrectly sized
413 // values into the implicitly aligned sections.
415 getStreamer().EmitValueToAlignment(Align);
420 /// parseDirectiveAltEntry
421 /// ::= .alt_entry identifier
422 bool DarwinAsmParser::parseDirectiveAltEntry(StringRef, SMLoc) {
424 if (getParser().parseIdentifier(Name))
425 return TokError("expected identifier in directive");
428 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
430 if (Sym->isDefined())
431 return TokError(".alt_entry must preceed symbol definition");
433 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_AltEntry))
434 return TokError("unable to emit symbol attribute");
440 /// parseDirectiveDesc
441 /// ::= .desc identifier , expression
442 bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
444 if (getParser().parseIdentifier(Name))
445 return TokError("expected identifier in directive");
447 // Handle the identifier as the key symbol.
448 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
450 if (getLexer().isNot(AsmToken::Comma))
451 return TokError("unexpected token in '.desc' directive");
455 if (getParser().parseAbsoluteExpression(DescValue))
458 if (getLexer().isNot(AsmToken::EndOfStatement))
459 return TokError("unexpected token in '.desc' directive");
463 // Set the n_desc field of this Symbol to this DescValue
464 getStreamer().EmitSymbolDesc(Sym, DescValue);
469 /// parseDirectiveIndirectSymbol
470 /// ::= .indirect_symbol identifier
471 bool DarwinAsmParser::parseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
472 const MCSectionMachO *Current = static_cast<const MCSectionMachO*>(
473 getStreamer().getCurrentSection().first);
474 MachO::SectionType SectionType = Current->getType();
475 if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
476 SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
477 SectionType != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS &&
478 SectionType != MachO::S_SYMBOL_STUBS)
479 return Error(Loc, "indirect symbol not in a symbol pointer or stub "
483 if (getParser().parseIdentifier(Name))
484 return TokError("expected identifier in .indirect_symbol directive");
486 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
488 // Assembler local symbols don't make any sense here. Complain loudly.
489 if (Sym->isTemporary())
490 return TokError("non-local symbol required in directive");
492 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
493 return TokError("unable to emit indirect symbol attribute for: " + Name);
495 if (getLexer().isNot(AsmToken::EndOfStatement))
496 return TokError("unexpected token in '.indirect_symbol' directive");
503 /// parseDirectiveDumpOrLoad
504 /// ::= ( .dump | .load ) "filename"
505 bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
507 bool IsDump = Directive == ".dump";
508 if (getLexer().isNot(AsmToken::String))
509 return TokError("expected string in '.dump' or '.load' directive");
513 if (getLexer().isNot(AsmToken::EndOfStatement))
514 return TokError("unexpected token in '.dump' or '.load' directive");
518 // FIXME: If/when .dump and .load are implemented they will be done in the
519 // the assembly parser and not have any need for an MCStreamer API.
521 return Warning(IDLoc, "ignoring directive .dump for now");
523 return Warning(IDLoc, "ignoring directive .load for now");
526 /// ParseDirectiveLinkerOption
527 /// ::= .linker_option "string" ( , "string" )*
528 bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
529 SmallVector<std::string, 4> Args;
531 if (getLexer().isNot(AsmToken::String))
532 return TokError("expected string in '" + Twine(IDVal) + "' directive");
535 if (getParser().parseEscapedString(Data))
538 Args.push_back(Data);
540 if (getLexer().is(AsmToken::EndOfStatement))
543 if (getLexer().isNot(AsmToken::Comma))
544 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
548 getStreamer().EmitLinkerOptions(Args);
552 /// parseDirectiveLsym
553 /// ::= .lsym identifier , expression
554 bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
556 if (getParser().parseIdentifier(Name))
557 return TokError("expected identifier in directive");
559 // Handle the identifier as the key symbol.
560 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
562 if (getLexer().isNot(AsmToken::Comma))
563 return TokError("unexpected token in '.lsym' directive");
567 if (getParser().parseExpression(Value))
570 if (getLexer().isNot(AsmToken::EndOfStatement))
571 return TokError("unexpected token in '.lsym' directive");
575 // We don't currently support this directive.
577 // FIXME: Diagnostic location!
579 return TokError("directive '.lsym' is unsupported");
582 /// parseDirectiveSection:
583 /// ::= .section identifier (',' identifier)*
584 bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
585 SMLoc Loc = getLexer().getLoc();
587 StringRef SectionName;
588 if (getParser().parseIdentifier(SectionName))
589 return Error(Loc, "expected identifier after '.section' directive");
591 // Verify there is a following comma.
592 if (!getLexer().is(AsmToken::Comma))
593 return TokError("unexpected token in '.section' directive");
595 std::string SectionSpec = SectionName;
598 // Add all the tokens until the end of the line, ParseSectionSpecifier will
600 StringRef EOL = getLexer().LexUntilEndOfStatement();
601 SectionSpec.append(EOL.begin(), EOL.end());
604 if (getLexer().isNot(AsmToken::EndOfStatement))
605 return TokError("unexpected token in '.section' directive");
609 StringRef Segment, Section;
613 std::string ErrorStr =
614 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
615 TAA, TAAParsed, StubSize);
617 if (!ErrorStr.empty())
618 return Error(Loc, ErrorStr.c_str());
620 // Issue a warning if the target is not powerpc and Section is a *coal* section.
621 Triple TT = getParser().getContext().getObjectFileInfo()->getTargetTriple();
622 Triple::ArchType ArchTy = TT.getArch();
624 if (ArchTy != Triple::ppc && ArchTy != Triple::ppc64) {
625 StringRef NonCoalSection = StringSwitch<StringRef>(Section)
626 .Case("__textcoal_nt", "__text")
627 .Case("__const_coal", "__const")
628 .Case("__datacoal_nt", "__data")
631 if (!Section.equals(NonCoalSection)) {
632 StringRef SectionVal(Loc.getPointer());
633 size_t B = SectionVal.find(',') + 1, E = SectionVal.find(',', B);
634 SMLoc BLoc = SMLoc::getFromPointer(SectionVal.data() + B);
635 SMLoc ELoc = SMLoc::getFromPointer(SectionVal.data() + E);
636 getParser().Warning(Loc, "section \"" + Section + "\" is deprecated",
637 SMRange(BLoc, ELoc));
638 getParser().Note(Loc, "change section name to \"" + NonCoalSection +
639 "\"", SMRange(BLoc, ELoc));
643 // FIXME: Arch specific.
644 bool isText = Segment == "__TEXT"; // FIXME: Hack.
645 getStreamer().SwitchSection(getContext().getMachOSection(
646 Segment, Section, TAA, StubSize,
647 isText ? SectionKind::getText() : SectionKind::getData()));
651 /// ParseDirectivePushSection:
652 /// ::= .pushsection identifier (',' identifier)*
653 bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
654 getStreamer().PushSection();
656 if (parseDirectiveSection(S, Loc)) {
657 getStreamer().PopSection();
664 /// ParseDirectivePopSection:
666 bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
667 if (!getStreamer().PopSection())
668 return TokError(".popsection without corresponding .pushsection");
672 /// ParseDirectivePrevious:
674 bool DarwinAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
675 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
676 if (!PreviousSection.first)
677 return TokError(".previous without corresponding .section");
678 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
682 /// ParseDirectiveSecureLogUnique
683 /// ::= .secure_log_unique ... message ...
684 bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
685 StringRef LogMessage = getParser().parseStringToEndOfStatement();
686 if (getLexer().isNot(AsmToken::EndOfStatement))
687 return TokError("unexpected token in '.secure_log_unique' directive");
689 if (getContext().getSecureLogUsed())
690 return Error(IDLoc, ".secure_log_unique specified multiple times");
692 // Get the secure log path.
693 const char *SecureLogFile = getContext().getSecureLogFile();
695 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
696 "environment variable unset.");
698 // Open the secure log file if we haven't already.
699 raw_fd_ostream *OS = getContext().getSecureLog();
702 auto NewOS = llvm::make_unique<raw_fd_ostream>(
703 SecureLogFile, EC, sys::fs::F_Append | sys::fs::F_Text);
705 return Error(IDLoc, Twine("can't open secure log file: ") +
706 SecureLogFile + " (" + EC.message() + ")");
708 getContext().setSecureLog(std::move(NewOS));
711 // Write the message.
712 unsigned CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
713 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
714 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
715 << LogMessage + "\n";
717 getContext().setSecureLogUsed(true);
722 /// ParseDirectiveSecureLogReset
723 /// ::= .secure_log_reset
724 bool DarwinAsmParser::parseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
725 if (getLexer().isNot(AsmToken::EndOfStatement))
726 return TokError("unexpected token in '.secure_log_reset' directive");
730 getContext().setSecureLogUsed(false);
735 /// parseDirectiveSubsectionsViaSymbols
736 /// ::= .subsections_via_symbols
737 bool DarwinAsmParser::parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
738 if (getLexer().isNot(AsmToken::EndOfStatement))
739 return TokError("unexpected token in '.subsections_via_symbols' directive");
743 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
748 /// ParseDirectiveTBSS
749 /// ::= .tbss identifier, size, align
750 bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
751 SMLoc IDLoc = getLexer().getLoc();
753 if (getParser().parseIdentifier(Name))
754 return TokError("expected identifier in directive");
756 // Handle the identifier as the key symbol.
757 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
759 if (getLexer().isNot(AsmToken::Comma))
760 return TokError("unexpected token in directive");
764 SMLoc SizeLoc = getLexer().getLoc();
765 if (getParser().parseAbsoluteExpression(Size))
768 int64_t Pow2Alignment = 0;
769 SMLoc Pow2AlignmentLoc;
770 if (getLexer().is(AsmToken::Comma)) {
772 Pow2AlignmentLoc = getLexer().getLoc();
773 if (getParser().parseAbsoluteExpression(Pow2Alignment))
777 if (getLexer().isNot(AsmToken::EndOfStatement))
778 return TokError("unexpected token in '.tbss' directive");
783 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
786 // FIXME: Diagnose overflow.
787 if (Pow2Alignment < 0)
788 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
791 if (!Sym->isUndefined())
792 return Error(IDLoc, "invalid symbol redefinition");
794 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
795 "__DATA", "__thread_bss",
796 MachO::S_THREAD_LOCAL_ZEROFILL,
797 0, SectionKind::getThreadBSS()),
798 Sym, Size, 1 << Pow2Alignment);
803 /// ParseDirectiveZerofill
804 /// ::= .zerofill segname , sectname [, identifier , size_expression [
805 /// , align_expression ]]
806 bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
808 if (getParser().parseIdentifier(Segment))
809 return TokError("expected segment name after '.zerofill' directive");
811 if (getLexer().isNot(AsmToken::Comma))
812 return TokError("unexpected token in directive");
816 if (getParser().parseIdentifier(Section))
817 return TokError("expected section name after comma in '.zerofill' "
820 // If this is the end of the line all that was wanted was to create the
821 // the section but with no symbol.
822 if (getLexer().is(AsmToken::EndOfStatement)) {
823 // Create the zerofill section but no symbol
824 getStreamer().EmitZerofill(getContext().getMachOSection(
825 Segment, Section, MachO::S_ZEROFILL,
826 0, SectionKind::getBSS()));
830 if (getLexer().isNot(AsmToken::Comma))
831 return TokError("unexpected token in directive");
834 SMLoc IDLoc = getLexer().getLoc();
836 if (getParser().parseIdentifier(IDStr))
837 return TokError("expected identifier in directive");
839 // handle the identifier as the key symbol.
840 MCSymbol *Sym = getContext().getOrCreateSymbol(IDStr);
842 if (getLexer().isNot(AsmToken::Comma))
843 return TokError("unexpected token in directive");
847 SMLoc SizeLoc = getLexer().getLoc();
848 if (getParser().parseAbsoluteExpression(Size))
851 int64_t Pow2Alignment = 0;
852 SMLoc Pow2AlignmentLoc;
853 if (getLexer().is(AsmToken::Comma)) {
855 Pow2AlignmentLoc = getLexer().getLoc();
856 if (getParser().parseAbsoluteExpression(Pow2Alignment))
860 if (getLexer().isNot(AsmToken::EndOfStatement))
861 return TokError("unexpected token in '.zerofill' directive");
866 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
869 // NOTE: The alignment in the directive is a power of 2 value, the assembler
870 // may internally end up wanting an alignment in bytes.
871 // FIXME: Diagnose overflow.
872 if (Pow2Alignment < 0)
873 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
874 "can't be less than zero");
876 if (!Sym->isUndefined())
877 return Error(IDLoc, "invalid symbol redefinition");
879 // Create the zerofill Symbol with Size and Pow2Alignment
881 // FIXME: Arch specific.
882 getStreamer().EmitZerofill(getContext().getMachOSection(
883 Segment, Section, MachO::S_ZEROFILL,
884 0, SectionKind::getBSS()),
885 Sym, Size, 1 << Pow2Alignment);
890 /// ParseDirectiveDataRegion
891 /// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
892 bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
893 if (getLexer().is(AsmToken::EndOfStatement)) {
895 getStreamer().EmitDataRegion(MCDR_DataRegion);
898 StringRef RegionType;
899 SMLoc Loc = getParser().getTok().getLoc();
900 if (getParser().parseIdentifier(RegionType))
901 return TokError("expected region type after '.data_region' directive");
902 int Kind = StringSwitch<int>(RegionType)
903 .Case("jt8", MCDR_DataRegionJT8)
904 .Case("jt16", MCDR_DataRegionJT16)
905 .Case("jt32", MCDR_DataRegionJT32)
908 return Error(Loc, "unknown region type in '.data_region' directive");
911 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
915 /// ParseDirectiveDataRegionEnd
916 /// ::= .end_data_region
917 bool DarwinAsmParser::parseDirectiveDataRegionEnd(StringRef, SMLoc) {
918 if (getLexer().isNot(AsmToken::EndOfStatement))
919 return TokError("unexpected token in '.end_data_region' directive");
922 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
927 /// ::= .ios_version_min major,minor[,update]
928 /// ::= .macosx_version_min major,minor[,update]
929 bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc Loc) {
930 int64_t Major = 0, Minor = 0, Update = 0;
931 int Kind = StringSwitch<int>(Directive)
932 .Case(".watchos_version_min", MCVM_WatchOSVersionMin)
933 .Case(".tvos_version_min", MCVM_TvOSVersionMin)
934 .Case(".ios_version_min", MCVM_IOSVersionMin)
935 .Case(".macosx_version_min", MCVM_OSXVersionMin);
936 // Get the major version number.
937 if (getLexer().isNot(AsmToken::Integer))
938 return TokError("invalid OS major version number");
939 Major = getLexer().getTok().getIntVal();
940 if (Major > 65535 || Major <= 0)
941 return TokError("invalid OS major version number");
943 if (getLexer().isNot(AsmToken::Comma))
944 return TokError("minor OS version number required, comma expected");
946 // Get the minor version number.
947 if (getLexer().isNot(AsmToken::Integer))
948 return TokError("invalid OS minor version number");
949 Minor = getLexer().getTok().getIntVal();
950 if (Minor > 255 || Minor < 0)
951 return TokError("invalid OS minor version number");
953 // Get the update level, if specified
954 if (getLexer().isNot(AsmToken::EndOfStatement)) {
955 if (getLexer().isNot(AsmToken::Comma))
956 return TokError("invalid update specifier, comma expected");
958 if (getLexer().isNot(AsmToken::Integer))
959 return TokError("invalid OS update number");
960 Update = getLexer().getTok().getIntVal();
961 if (Update > 255 || Update < 0)
962 return TokError("invalid OS update number");
966 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
967 Triple::OSType ExpectedOS = Triple::UnknownOS;
968 switch ((MCVersionMinType)Kind) {
969 case MCVM_WatchOSVersionMin: ExpectedOS = Triple::WatchOS; break;
970 case MCVM_TvOSVersionMin: ExpectedOS = Triple::TvOS; break;
971 case MCVM_IOSVersionMin: ExpectedOS = Triple::IOS; break;
972 case MCVM_OSXVersionMin: ExpectedOS = Triple::MacOSX; break;
974 if (T.getOS() != ExpectedOS)
975 Warning(Loc, Directive + " should only be used for " +
976 Triple::getOSTypeName(ExpectedOS) + " targets");
978 if (LastVersionMinDirective.isValid()) {
979 Warning(Loc, "overriding previous version_min directive");
980 Note(LastVersionMinDirective, "previous definition is here");
982 LastVersionMinDirective = Loc;
984 // We've parsed a correct version specifier, so send it to the streamer.
985 getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
992 MCAsmParserExtension *createDarwinAsmParser() {
993 return new DarwinAsmParser;
996 } // end llvm namespace