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/Twine.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCParser/MCAsmLexer.h"
16 #include "llvm/MC/MCParser/MCAsmParser.h"
17 #include "llvm/MC/MCSectionMachO.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/SourceMgr.h"
27 /// \brief Implementation of directive handling which is shared across all
29 class DarwinAsmParser : public MCAsmParserExtension {
30 template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
31 void addDirectiveHandler(StringRef Directive) {
32 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
33 this, HandleDirective<DarwinAsmParser, HandlerMethod>);
34 getParser().addDirectiveHandler(Directive, Handler);
37 bool parseSectionSwitch(const char *Segment, const char *Section,
38 unsigned TAA = 0, unsigned ImplicitAlign = 0,
39 unsigned StubSize = 0);
44 void Initialize(MCAsmParser &Parser) override {
45 // Call the base implementation.
46 this->MCAsmParserExtension::Initialize(Parser);
48 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(".desc");
49 addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
51 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLsym>(".lsym");
52 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSubsectionsViaSymbols>(
53 ".subsections_via_symbols");
54 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".dump");
55 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".load");
56 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSection>(".section");
57 addDirectiveHandler<&DarwinAsmParser::parseDirectivePushSection>(
59 addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
61 addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(".previous");
62 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
63 ".secure_log_unique");
64 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
66 addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(".tbss");
67 addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(".zerofill");
69 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
71 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
74 // Special section directives.
75 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(".bss");
76 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(".const");
77 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
79 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
81 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
83 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(".data");
84 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
86 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(".dyld");
87 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
89 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
92 &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
93 ".lazy_symbol_pointer");
94 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
96 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
98 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
100 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
102 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
104 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
107 &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
108 ".non_lazy_symbol_pointer");
109 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatClsMeth>(
110 ".objc_cat_cls_meth");
111 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatInstMeth>(
112 ".objc_cat_inst_meth");
113 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCategory>(
115 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
117 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
118 ".objc_class_names");
119 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
121 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
123 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
125 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
128 &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
129 ".objc_instance_vars");
130 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
131 ".objc_message_refs");
132 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
135 &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
136 ".objc_meth_var_names");
138 &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
139 ".objc_meth_var_types");
140 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
141 ".objc_module_info");
142 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
145 &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
146 ".objc_selector_strs");
148 &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
149 ".objc_string_object");
150 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
152 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
154 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
156 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
158 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
160 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(".tdata");
161 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(".text");
162 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
163 ".thread_init_func");
164 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv");
166 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveIdent>(".ident");
167 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".ios_version_min");
168 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
169 ".macosx_version_min");
172 bool parseDirectiveDesc(StringRef, SMLoc);
173 bool parseDirectiveIndirectSymbol(StringRef, SMLoc);
174 bool parseDirectiveDumpOrLoad(StringRef, SMLoc);
175 bool parseDirectiveLsym(StringRef, SMLoc);
176 bool parseDirectiveLinkerOption(StringRef, SMLoc);
177 bool parseDirectiveSection(StringRef, SMLoc);
178 bool parseDirectivePushSection(StringRef, SMLoc);
179 bool parseDirectivePopSection(StringRef, SMLoc);
180 bool parseDirectivePrevious(StringRef, SMLoc);
181 bool parseDirectiveSecureLogReset(StringRef, SMLoc);
182 bool parseDirectiveSecureLogUnique(StringRef, SMLoc);
183 bool parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
184 bool parseDirectiveTBSS(StringRef, SMLoc);
185 bool parseDirectiveZerofill(StringRef, SMLoc);
186 bool parseDirectiveDataRegion(StringRef, SMLoc);
187 bool parseDirectiveDataRegionEnd(StringRef, SMLoc);
189 // Named Section Directive
190 bool parseSectionDirectiveBss(StringRef, SMLoc) {
191 return parseSectionSwitch("__DATA", "__bss");
194 bool parseSectionDirectiveConst(StringRef, SMLoc) {
195 return parseSectionSwitch("__TEXT", "__const");
197 bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
198 return parseSectionSwitch("__TEXT", "__static_const");
200 bool parseSectionDirectiveCString(StringRef, SMLoc) {
201 return parseSectionSwitch("__TEXT","__cstring",
202 MachO::S_CSTRING_LITERALS);
204 bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
205 return parseSectionSwitch("__TEXT", "__literal4",
206 MachO::S_4BYTE_LITERALS, 4);
208 bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
209 return parseSectionSwitch("__TEXT", "__literal8",
210 MachO::S_8BYTE_LITERALS, 8);
212 bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
213 return parseSectionSwitch("__TEXT","__literal16",
214 MachO::S_16BYTE_LITERALS, 16);
216 bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
217 return parseSectionSwitch("__TEXT","__constructor");
219 bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
220 return parseSectionSwitch("__TEXT","__destructor");
222 bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
223 return parseSectionSwitch("__TEXT","__fvmlib_init0");
225 bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
226 return parseSectionSwitch("__TEXT","__fvmlib_init1");
228 bool parseSectionDirectiveSymbolStub(StringRef, SMLoc) {
229 return parseSectionSwitch("__TEXT","__symbol_stub",
230 MachO::S_SYMBOL_STUBS |
231 MachO::S_ATTR_PURE_INSTRUCTIONS,
232 // FIXME: Different on PPC and ARM.
235 bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
236 return parseSectionSwitch("__TEXT","__picsymbol_stub",
237 MachO::S_SYMBOL_STUBS |
238 MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
240 bool parseSectionDirectiveData(StringRef, SMLoc) {
241 return parseSectionSwitch("__DATA", "__data");
243 bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
244 return parseSectionSwitch("__DATA", "__static_data");
246 bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
247 return parseSectionSwitch("__DATA", "__nl_symbol_ptr",
248 MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
250 bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
251 return parseSectionSwitch("__DATA", "__la_symbol_ptr",
252 MachO::S_LAZY_SYMBOL_POINTERS, 4);
254 bool parseSectionDirectiveDyld(StringRef, SMLoc) {
255 return parseSectionSwitch("__DATA", "__dyld");
257 bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
258 return parseSectionSwitch("__DATA", "__mod_init_func",
259 MachO::S_MOD_INIT_FUNC_POINTERS, 4);
261 bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
262 return parseSectionSwitch("__DATA", "__mod_term_func",
263 MachO::S_MOD_TERM_FUNC_POINTERS, 4);
265 bool parseSectionDirectiveConstData(StringRef, SMLoc) {
266 return parseSectionSwitch("__DATA", "__const");
268 bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
269 return parseSectionSwitch("__OBJC", "__class",
270 MachO::S_ATTR_NO_DEAD_STRIP);
272 bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
273 return parseSectionSwitch("__OBJC", "__meta_class",
274 MachO::S_ATTR_NO_DEAD_STRIP);
276 bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
277 return parseSectionSwitch("__OBJC", "__cat_cls_meth",
278 MachO::S_ATTR_NO_DEAD_STRIP);
280 bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
281 return parseSectionSwitch("__OBJC", "__cat_inst_meth",
282 MachO::S_ATTR_NO_DEAD_STRIP);
284 bool parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
285 return parseSectionSwitch("__OBJC", "__protocol",
286 MachO::S_ATTR_NO_DEAD_STRIP);
288 bool parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
289 return parseSectionSwitch("__OBJC", "__string_object",
290 MachO::S_ATTR_NO_DEAD_STRIP);
292 bool parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
293 return parseSectionSwitch("__OBJC", "__cls_meth",
294 MachO::S_ATTR_NO_DEAD_STRIP);
296 bool parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
297 return parseSectionSwitch("__OBJC", "__inst_meth",
298 MachO::S_ATTR_NO_DEAD_STRIP);
300 bool parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
301 return parseSectionSwitch("__OBJC", "__cls_refs",
302 MachO::S_ATTR_NO_DEAD_STRIP |
303 MachO::S_LITERAL_POINTERS, 4);
305 bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
306 return parseSectionSwitch("__OBJC", "__message_refs",
307 MachO::S_ATTR_NO_DEAD_STRIP |
308 MachO::S_LITERAL_POINTERS, 4);
310 bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
311 return parseSectionSwitch("__OBJC", "__symbols",
312 MachO::S_ATTR_NO_DEAD_STRIP);
314 bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
315 return parseSectionSwitch("__OBJC", "__category",
316 MachO::S_ATTR_NO_DEAD_STRIP);
318 bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
319 return parseSectionSwitch("__OBJC", "__class_vars",
320 MachO::S_ATTR_NO_DEAD_STRIP);
322 bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
323 return parseSectionSwitch("__OBJC", "__instance_vars",
324 MachO::S_ATTR_NO_DEAD_STRIP);
326 bool parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
327 return parseSectionSwitch("__OBJC", "__module_info",
328 MachO::S_ATTR_NO_DEAD_STRIP);
330 bool parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
331 return parseSectionSwitch("__TEXT", "__cstring",
332 MachO::S_CSTRING_LITERALS);
334 bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
335 return parseSectionSwitch("__TEXT", "__cstring",
336 MachO::S_CSTRING_LITERALS);
338 bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
339 return parseSectionSwitch("__TEXT", "__cstring",
340 MachO::S_CSTRING_LITERALS);
342 bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
343 return parseSectionSwitch("__OBJC", "__selector_strs",
344 MachO::S_CSTRING_LITERALS);
346 bool parseSectionDirectiveTData(StringRef, SMLoc) {
347 return parseSectionSwitch("__DATA", "__thread_data",
348 MachO::S_THREAD_LOCAL_REGULAR);
350 bool parseSectionDirectiveText(StringRef, SMLoc) {
351 return parseSectionSwitch("__TEXT", "__text",
352 MachO::S_ATTR_PURE_INSTRUCTIONS);
354 bool parseSectionDirectiveTLV(StringRef, SMLoc) {
355 return parseSectionSwitch("__DATA", "__thread_vars",
356 MachO::S_THREAD_LOCAL_VARIABLES);
358 bool parseSectionDirectiveIdent(StringRef, SMLoc) {
359 // Darwin silently ignores the .ident directive.
360 getParser().eatToEndOfStatement();
363 bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
364 return parseSectionSwitch("__DATA", "__thread_init",
365 MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
367 bool parseVersionMin(StringRef, SMLoc);
371 } // end anonymous namespace
373 bool DarwinAsmParser::parseSectionSwitch(const char *Segment,
375 unsigned TAA, unsigned Align,
377 if (getLexer().isNot(AsmToken::EndOfStatement))
378 return TokError("unexpected token in section switching directive");
381 // FIXME: Arch specific.
382 bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
383 getStreamer().SwitchSection(getContext().getMachOSection(
384 Segment, Section, TAA, StubSize,
385 isText ? SectionKind::getText()
386 : SectionKind::getDataRel()));
388 // Set the implicit alignment, if any.
390 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
391 // alignment on the section (e.g., if one manually inserts bytes into the
392 // section, then just issuing the section switch directive will not realign
393 // the section. However, this is arguably more reasonable behavior, and there
394 // is no good reason for someone to intentionally emit incorrectly sized
395 // values into the implicitly aligned sections.
397 getStreamer().EmitValueToAlignment(Align);
402 /// parseDirectiveDesc
403 /// ::= .desc identifier , expression
404 bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
406 if (getParser().parseIdentifier(Name))
407 return TokError("expected identifier in directive");
409 // Handle the identifier as the key symbol.
410 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
412 if (getLexer().isNot(AsmToken::Comma))
413 return TokError("unexpected token in '.desc' directive");
417 if (getParser().parseAbsoluteExpression(DescValue))
420 if (getLexer().isNot(AsmToken::EndOfStatement))
421 return TokError("unexpected token in '.desc' directive");
425 // Set the n_desc field of this Symbol to this DescValue
426 getStreamer().EmitSymbolDesc(Sym, DescValue);
431 /// parseDirectiveIndirectSymbol
432 /// ::= .indirect_symbol identifier
433 bool DarwinAsmParser::parseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
434 const MCSectionMachO *Current = static_cast<const MCSectionMachO*>(
435 getStreamer().getCurrentSection().first);
436 MachO::SectionType SectionType = Current->getType();
437 if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
438 SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
439 SectionType != MachO::S_SYMBOL_STUBS)
440 return Error(Loc, "indirect symbol not in a symbol pointer or stub "
444 if (getParser().parseIdentifier(Name))
445 return TokError("expected identifier in .indirect_symbol directive");
447 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
449 // Assembler local symbols don't make any sense here. Complain loudly.
450 if (Sym->isTemporary())
451 return TokError("non-local symbol required in directive");
453 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
454 return TokError("unable to emit indirect symbol attribute for: " + Name);
456 if (getLexer().isNot(AsmToken::EndOfStatement))
457 return TokError("unexpected token in '.indirect_symbol' directive");
464 /// parseDirectiveDumpOrLoad
465 /// ::= ( .dump | .load ) "filename"
466 bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
468 bool IsDump = Directive == ".dump";
469 if (getLexer().isNot(AsmToken::String))
470 return TokError("expected string in '.dump' or '.load' directive");
474 if (getLexer().isNot(AsmToken::EndOfStatement))
475 return TokError("unexpected token in '.dump' or '.load' directive");
479 // FIXME: If/when .dump and .load are implemented they will be done in the
480 // the assembly parser and not have any need for an MCStreamer API.
482 return Warning(IDLoc, "ignoring directive .dump for now");
484 return Warning(IDLoc, "ignoring directive .load for now");
487 /// ParseDirectiveLinkerOption
488 /// ::= .linker_option "string" ( , "string" )*
489 bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
490 SmallVector<std::string, 4> Args;
492 if (getLexer().isNot(AsmToken::String))
493 return TokError("expected string in '" + Twine(IDVal) + "' directive");
496 if (getParser().parseEscapedString(Data))
499 Args.push_back(Data);
502 if (getLexer().is(AsmToken::EndOfStatement))
505 if (getLexer().isNot(AsmToken::Comma))
506 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
510 getStreamer().EmitLinkerOptions(Args);
514 /// parseDirectiveLsym
515 /// ::= .lsym identifier , expression
516 bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
518 if (getParser().parseIdentifier(Name))
519 return TokError("expected identifier in directive");
521 // Handle the identifier as the key symbol.
522 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
524 if (getLexer().isNot(AsmToken::Comma))
525 return TokError("unexpected token in '.lsym' directive");
529 if (getParser().parseExpression(Value))
532 if (getLexer().isNot(AsmToken::EndOfStatement))
533 return TokError("unexpected token in '.lsym' directive");
537 // We don't currently support this directive.
539 // FIXME: Diagnostic location!
541 return TokError("directive '.lsym' is unsupported");
544 /// parseDirectiveSection:
545 /// ::= .section identifier (',' identifier)*
546 bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
547 SMLoc Loc = getLexer().getLoc();
549 StringRef SectionName;
550 if (getParser().parseIdentifier(SectionName))
551 return Error(Loc, "expected identifier after '.section' directive");
553 // Verify there is a following comma.
554 if (!getLexer().is(AsmToken::Comma))
555 return TokError("unexpected token in '.section' directive");
557 std::string SectionSpec = SectionName;
560 // Add all the tokens until the end of the line, ParseSectionSpecifier will
562 StringRef EOL = getLexer().LexUntilEndOfStatement();
563 SectionSpec.append(EOL.begin(), EOL.end());
566 if (getLexer().isNot(AsmToken::EndOfStatement))
567 return TokError("unexpected token in '.section' directive");
571 StringRef Segment, Section;
575 std::string ErrorStr =
576 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
577 TAA, TAAParsed, StubSize);
579 if (!ErrorStr.empty())
580 return Error(Loc, ErrorStr.c_str());
582 // FIXME: Arch specific.
583 bool isText = Segment == "__TEXT"; // FIXME: Hack.
584 getStreamer().SwitchSection(getContext().getMachOSection(
585 Segment, Section, TAA, StubSize,
586 isText ? SectionKind::getText()
587 : SectionKind::getDataRel()));
591 /// ParseDirectivePushSection:
592 /// ::= .pushsection identifier (',' identifier)*
593 bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
594 getStreamer().PushSection();
596 if (parseDirectiveSection(S, Loc)) {
597 getStreamer().PopSection();
604 /// ParseDirectivePopSection:
606 bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
607 if (!getStreamer().PopSection())
608 return TokError(".popsection without corresponding .pushsection");
612 /// ParseDirectivePrevious:
614 bool DarwinAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
615 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
616 if (!PreviousSection.first)
617 return TokError(".previous without corresponding .section");
618 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
622 /// ParseDirectiveSecureLogUnique
623 /// ::= .secure_log_unique ... message ...
624 bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
625 StringRef LogMessage = getParser().parseStringToEndOfStatement();
626 if (getLexer().isNot(AsmToken::EndOfStatement))
627 return TokError("unexpected token in '.secure_log_unique' directive");
629 if (getContext().getSecureLogUsed() != false)
630 return Error(IDLoc, ".secure_log_unique specified multiple times");
632 // Get the secure log path.
633 const char *SecureLogFile = getContext().getSecureLogFile();
635 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
636 "environment variable unset.");
638 // Open the secure log file if we haven't already.
639 raw_ostream *OS = getContext().getSecureLog();
642 OS = new raw_fd_ostream(SecureLogFile, Err,
643 sys::fs::F_Append | sys::fs::F_Text);
646 return Error(IDLoc, Twine("can't open secure log file: ") +
647 SecureLogFile + " (" + Err + ")");
649 getContext().setSecureLog(OS);
652 // Write the message.
653 unsigned CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
654 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
655 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
656 << LogMessage + "\n";
658 getContext().setSecureLogUsed(true);
663 /// ParseDirectiveSecureLogReset
664 /// ::= .secure_log_reset
665 bool DarwinAsmParser::parseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
666 if (getLexer().isNot(AsmToken::EndOfStatement))
667 return TokError("unexpected token in '.secure_log_reset' directive");
671 getContext().setSecureLogUsed(false);
676 /// parseDirectiveSubsectionsViaSymbols
677 /// ::= .subsections_via_symbols
678 bool DarwinAsmParser::parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
679 if (getLexer().isNot(AsmToken::EndOfStatement))
680 return TokError("unexpected token in '.subsections_via_symbols' directive");
684 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
689 /// ParseDirectiveTBSS
690 /// ::= .tbss identifier, size, align
691 bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
692 SMLoc IDLoc = getLexer().getLoc();
694 if (getParser().parseIdentifier(Name))
695 return TokError("expected identifier in directive");
697 // Handle the identifier as the key symbol.
698 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
700 if (getLexer().isNot(AsmToken::Comma))
701 return TokError("unexpected token in directive");
705 SMLoc SizeLoc = getLexer().getLoc();
706 if (getParser().parseAbsoluteExpression(Size))
709 int64_t Pow2Alignment = 0;
710 SMLoc Pow2AlignmentLoc;
711 if (getLexer().is(AsmToken::Comma)) {
713 Pow2AlignmentLoc = getLexer().getLoc();
714 if (getParser().parseAbsoluteExpression(Pow2Alignment))
718 if (getLexer().isNot(AsmToken::EndOfStatement))
719 return TokError("unexpected token in '.tbss' directive");
724 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
727 // FIXME: Diagnose overflow.
728 if (Pow2Alignment < 0)
729 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
732 if (!Sym->isUndefined())
733 return Error(IDLoc, "invalid symbol redefinition");
735 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
736 "__DATA", "__thread_bss",
737 MachO::S_THREAD_LOCAL_ZEROFILL,
738 0, SectionKind::getThreadBSS()),
739 Sym, Size, 1 << Pow2Alignment);
744 /// ParseDirectiveZerofill
745 /// ::= .zerofill segname , sectname [, identifier , size_expression [
746 /// , align_expression ]]
747 bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
749 if (getParser().parseIdentifier(Segment))
750 return TokError("expected segment name after '.zerofill' directive");
752 if (getLexer().isNot(AsmToken::Comma))
753 return TokError("unexpected token in directive");
757 if (getParser().parseIdentifier(Section))
758 return TokError("expected section name after comma in '.zerofill' "
761 // If this is the end of the line all that was wanted was to create the
762 // the section but with no symbol.
763 if (getLexer().is(AsmToken::EndOfStatement)) {
764 // Create the zerofill section but no symbol
765 getStreamer().EmitZerofill(getContext().getMachOSection(
766 Segment, Section, MachO::S_ZEROFILL,
767 0, SectionKind::getBSS()));
771 if (getLexer().isNot(AsmToken::Comma))
772 return TokError("unexpected token in directive");
775 SMLoc IDLoc = getLexer().getLoc();
777 if (getParser().parseIdentifier(IDStr))
778 return TokError("expected identifier in directive");
780 // handle the identifier as the key symbol.
781 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
783 if (getLexer().isNot(AsmToken::Comma))
784 return TokError("unexpected token in directive");
788 SMLoc SizeLoc = getLexer().getLoc();
789 if (getParser().parseAbsoluteExpression(Size))
792 int64_t Pow2Alignment = 0;
793 SMLoc Pow2AlignmentLoc;
794 if (getLexer().is(AsmToken::Comma)) {
796 Pow2AlignmentLoc = getLexer().getLoc();
797 if (getParser().parseAbsoluteExpression(Pow2Alignment))
801 if (getLexer().isNot(AsmToken::EndOfStatement))
802 return TokError("unexpected token in '.zerofill' directive");
807 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
810 // NOTE: The alignment in the directive is a power of 2 value, the assembler
811 // may internally end up wanting an alignment in bytes.
812 // FIXME: Diagnose overflow.
813 if (Pow2Alignment < 0)
814 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
815 "can't be less than zero");
817 if (!Sym->isUndefined())
818 return Error(IDLoc, "invalid symbol redefinition");
820 // Create the zerofill Symbol with Size and Pow2Alignment
822 // FIXME: Arch specific.
823 getStreamer().EmitZerofill(getContext().getMachOSection(
824 Segment, Section, MachO::S_ZEROFILL,
825 0, SectionKind::getBSS()),
826 Sym, Size, 1 << Pow2Alignment);
831 /// ParseDirectiveDataRegion
832 /// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
833 bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
834 if (getLexer().is(AsmToken::EndOfStatement)) {
836 getStreamer().EmitDataRegion(MCDR_DataRegion);
839 StringRef RegionType;
840 SMLoc Loc = getParser().getTok().getLoc();
841 if (getParser().parseIdentifier(RegionType))
842 return TokError("expected region type after '.data_region' directive");
843 int Kind = StringSwitch<int>(RegionType)
844 .Case("jt8", MCDR_DataRegionJT8)
845 .Case("jt16", MCDR_DataRegionJT16)
846 .Case("jt32", MCDR_DataRegionJT32)
849 return Error(Loc, "unknown region type in '.data_region' directive");
852 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
856 /// ParseDirectiveDataRegionEnd
857 /// ::= .end_data_region
858 bool DarwinAsmParser::parseDirectiveDataRegionEnd(StringRef, SMLoc) {
859 if (getLexer().isNot(AsmToken::EndOfStatement))
860 return TokError("unexpected token in '.end_data_region' directive");
863 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
868 /// ::= .ios_version_min major,minor[,update]
869 /// ::= .macosx_version_min major,minor[,update]
870 bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc) {
871 int64_t Major = 0, Minor = 0, Update = 0;
872 int Kind = StringSwitch<int>(Directive)
873 .Case(".ios_version_min", MCVM_IOSVersionMin)
874 .Case(".macosx_version_min", MCVM_OSXVersionMin);
875 // Get the major version number.
876 if (getLexer().isNot(AsmToken::Integer))
877 return TokError("invalid OS major version number");
878 Major = getLexer().getTok().getIntVal();
879 if (Major > 65535 || Major <= 0)
880 return TokError("invalid OS major version number");
882 if (getLexer().isNot(AsmToken::Comma))
883 return TokError("minor OS version number required, comma expected");
885 // Get the minor version number.
886 if (getLexer().isNot(AsmToken::Integer))
887 return TokError("invalid OS minor version number");
888 Minor = getLexer().getTok().getIntVal();
889 if (Minor > 255 || Minor < 0)
890 return TokError("invalid OS minor version number");
892 // Get the update level, if specified
893 if (getLexer().isNot(AsmToken::EndOfStatement)) {
894 if (getLexer().isNot(AsmToken::Comma))
895 return TokError("invalid update specifier, comma expected");
897 if (getLexer().isNot(AsmToken::Integer))
898 return TokError("invalid OS update number");
899 Update = getLexer().getTok().getIntVal();
900 if (Update > 255 || Update < 0)
901 return TokError("invalid OS update number");
905 // We've parsed a correct version specifier, so send it to the streamer.
906 getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
913 MCAsmParserExtension *createDarwinAsmParser() {
914 return new DarwinAsmParser;
917 } // end llvm namespace