]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/MC/MCParser/DarwinAsmParser.cpp
Upgrade to OpenSSH 7.3p1.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / MC / MCParser / DarwinAsmParser.cpp
1 //===- DarwinAsmParser.cpp - Darwin (Mach-O) Assembly Parser --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
11 #include "llvm/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"
26 using namespace llvm;
27
28 namespace {
29
30 /// \brief Implementation of directive handling which is shared across all
31 /// Darwin targets.
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);
38   }
39
40   bool parseSectionSwitch(const char *Segment, const char *Section,
41                           unsigned TAA = 0, unsigned ImplicitAlign = 0,
42                           unsigned StubSize = 0);
43
44   SMLoc LastVersionMinDirective;
45
46 public:
47   DarwinAsmParser() {}
48
49   void Initialize(MCAsmParser &Parser) override {
50     // Call the base implementation.
51     this->MCAsmParserExtension::Initialize(Parser);
52
53     addDirectiveHandler<&DarwinAsmParser::parseDirectiveAltEntry>(".alt_entry");
54     addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(".desc");
55     addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
56       ".indirect_symbol");
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>(
64       ".pushsection");
65     addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
66       ".popsection");
67     addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(".previous");
68     addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
69       ".secure_log_unique");
70     addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
71       ".secure_log_reset");
72     addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(".tbss");
73     addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(".zerofill");
74
75     addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
76       ".data_region");
77     addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
78       ".end_data_region");
79
80     // Special section directives.
81     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(".bss");
82     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(".const");
83     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
84       ".const_data");
85     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
86       ".constructor");
87     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
88       ".cstring");
89     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(".data");
90     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
91       ".destructor");
92     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(".dyld");
93     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
94       ".fvmlib_init0");
95     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
96       ".fvmlib_init1");
97     addDirectiveHandler<
98       &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
99         ".lazy_symbol_pointer");
100     addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
101       ".linker_option");
102     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
103       ".literal16");
104     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
105       ".literal4");
106     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
107       ".literal8");
108     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
109       ".mod_init_func");
110     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
111       ".mod_term_func");
112     addDirectiveHandler<
113       &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
114         ".non_lazy_symbol_pointer");
115     addDirectiveHandler<
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>(
123       ".objc_category");
124     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
125       ".objc_class");
126     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
127       ".objc_class_names");
128     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
129       ".objc_class_vars");
130     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
131       ".objc_cls_meth");
132     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
133       ".objc_cls_refs");
134     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
135       ".objc_inst_meth");
136     addDirectiveHandler<
137       &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
138         ".objc_instance_vars");
139     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
140       ".objc_message_refs");
141     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
142       ".objc_meta_class");
143     addDirectiveHandler<
144       &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
145         ".objc_meth_var_names");
146     addDirectiveHandler<
147       &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
148         ".objc_meth_var_types");
149     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
150       ".objc_module_info");
151     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
152       ".objc_protocol");
153     addDirectiveHandler<
154       &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
155         ".objc_selector_strs");
156     addDirectiveHandler<
157       &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
158         ".objc_string_object");
159     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
160       ".objc_symbols");
161     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
162       ".picsymbol_stub");
163     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
164       ".static_const");
165     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
166       ".static_data");
167     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
168       ".symbol_stub");
169     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(".tdata");
170     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(".text");
171     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
172       ".thread_init_func");
173     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv");
174
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");
182
183     LastVersionMinDirective = SMLoc();
184   }
185
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);
203
204   // Named Section Directive
205   bool parseSectionDirectiveBss(StringRef, SMLoc) {
206     return parseSectionSwitch("__DATA", "__bss");
207   }
208
209   bool parseSectionDirectiveConst(StringRef, SMLoc) {
210     return parseSectionSwitch("__TEXT", "__const");
211   }
212   bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
213     return parseSectionSwitch("__TEXT", "__static_const");
214   }
215   bool parseSectionDirectiveCString(StringRef, SMLoc) {
216     return parseSectionSwitch("__TEXT","__cstring",
217                               MachO::S_CSTRING_LITERALS);
218   }
219   bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
220     return parseSectionSwitch("__TEXT", "__literal4",
221                               MachO::S_4BYTE_LITERALS, 4);
222   }
223   bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
224     return parseSectionSwitch("__TEXT", "__literal8",
225                               MachO::S_8BYTE_LITERALS, 8);
226   }
227   bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
228     return parseSectionSwitch("__TEXT","__literal16",
229                               MachO::S_16BYTE_LITERALS, 16);
230   }
231   bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
232     return parseSectionSwitch("__TEXT","__constructor");
233   }
234   bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
235     return parseSectionSwitch("__TEXT","__destructor");
236   }
237   bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
238     return parseSectionSwitch("__TEXT","__fvmlib_init0");
239   }
240   bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
241     return parseSectionSwitch("__TEXT","__fvmlib_init1");
242   }
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.
248                               0, 16);
249   }
250   bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
251     return parseSectionSwitch("__TEXT","__picsymbol_stub",
252                               MachO::S_SYMBOL_STUBS |
253                               MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
254   }
255   bool parseSectionDirectiveData(StringRef, SMLoc) {
256     return parseSectionSwitch("__DATA", "__data");
257   }
258   bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
259     return parseSectionSwitch("__DATA", "__static_data");
260   }
261   bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
262     return parseSectionSwitch("__DATA", "__nl_symbol_ptr",
263                               MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
264   }
265   bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
266     return parseSectionSwitch("__DATA", "__la_symbol_ptr",
267                               MachO::S_LAZY_SYMBOL_POINTERS, 4);
268   }
269   bool parseSectionDirectiveThreadLocalVariablePointers(StringRef, SMLoc) {
270     return parseSectionSwitch("__DATA", "__thread_ptr",
271                               MachO::S_THREAD_LOCAL_VARIABLE_POINTERS, 4);
272   }
273   bool parseSectionDirectiveDyld(StringRef, SMLoc) {
274     return parseSectionSwitch("__DATA", "__dyld");
275   }
276   bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
277     return parseSectionSwitch("__DATA", "__mod_init_func",
278                               MachO::S_MOD_INIT_FUNC_POINTERS, 4);
279   }
280   bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
281     return parseSectionSwitch("__DATA", "__mod_term_func",
282                               MachO::S_MOD_TERM_FUNC_POINTERS, 4);
283   }
284   bool parseSectionDirectiveConstData(StringRef, SMLoc) {
285     return parseSectionSwitch("__DATA", "__const");
286   }
287   bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
288     return parseSectionSwitch("__OBJC", "__class",
289                               MachO::S_ATTR_NO_DEAD_STRIP);
290   }
291   bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
292     return parseSectionSwitch("__OBJC", "__meta_class",
293                               MachO::S_ATTR_NO_DEAD_STRIP);
294   }
295   bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
296     return parseSectionSwitch("__OBJC", "__cat_cls_meth",
297                               MachO::S_ATTR_NO_DEAD_STRIP);
298   }
299   bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
300     return parseSectionSwitch("__OBJC", "__cat_inst_meth",
301                               MachO::S_ATTR_NO_DEAD_STRIP);
302   }
303   bool parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
304     return parseSectionSwitch("__OBJC", "__protocol",
305                               MachO::S_ATTR_NO_DEAD_STRIP);
306   }
307   bool parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
308     return parseSectionSwitch("__OBJC", "__string_object",
309                               MachO::S_ATTR_NO_DEAD_STRIP);
310   }
311   bool parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
312     return parseSectionSwitch("__OBJC", "__cls_meth",
313                               MachO::S_ATTR_NO_DEAD_STRIP);
314   }
315   bool parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
316     return parseSectionSwitch("__OBJC", "__inst_meth",
317                               MachO::S_ATTR_NO_DEAD_STRIP);
318   }
319   bool parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
320     return parseSectionSwitch("__OBJC", "__cls_refs",
321                               MachO::S_ATTR_NO_DEAD_STRIP |
322                               MachO::S_LITERAL_POINTERS, 4);
323   }
324   bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
325     return parseSectionSwitch("__OBJC", "__message_refs",
326                               MachO::S_ATTR_NO_DEAD_STRIP |
327                               MachO::S_LITERAL_POINTERS, 4);
328   }
329   bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
330     return parseSectionSwitch("__OBJC", "__symbols",
331                               MachO::S_ATTR_NO_DEAD_STRIP);
332   }
333   bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
334     return parseSectionSwitch("__OBJC", "__category",
335                               MachO::S_ATTR_NO_DEAD_STRIP);
336   }
337   bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
338     return parseSectionSwitch("__OBJC", "__class_vars",
339                               MachO::S_ATTR_NO_DEAD_STRIP);
340   }
341   bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
342     return parseSectionSwitch("__OBJC", "__instance_vars",
343                               MachO::S_ATTR_NO_DEAD_STRIP);
344   }
345   bool parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
346     return parseSectionSwitch("__OBJC", "__module_info",
347                               MachO::S_ATTR_NO_DEAD_STRIP);
348   }
349   bool parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
350     return parseSectionSwitch("__TEXT", "__cstring",
351                               MachO::S_CSTRING_LITERALS);
352   }
353   bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
354     return parseSectionSwitch("__TEXT", "__cstring",
355                               MachO::S_CSTRING_LITERALS);
356   }
357   bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
358     return parseSectionSwitch("__TEXT", "__cstring",
359                               MachO::S_CSTRING_LITERALS);
360   }
361   bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
362     return parseSectionSwitch("__OBJC", "__selector_strs",
363                               MachO::S_CSTRING_LITERALS);
364   }
365   bool parseSectionDirectiveTData(StringRef, SMLoc) {
366     return parseSectionSwitch("__DATA", "__thread_data",
367                               MachO::S_THREAD_LOCAL_REGULAR);
368   }
369   bool parseSectionDirectiveText(StringRef, SMLoc) {
370     return parseSectionSwitch("__TEXT", "__text",
371                               MachO::S_ATTR_PURE_INSTRUCTIONS);
372   }
373   bool parseSectionDirectiveTLV(StringRef, SMLoc) {
374     return parseSectionSwitch("__DATA", "__thread_vars",
375                               MachO::S_THREAD_LOCAL_VARIABLES);
376   }
377   bool parseSectionDirectiveIdent(StringRef, SMLoc) {
378     // Darwin silently ignores the .ident directive.
379     getParser().eatToEndOfStatement();
380     return false;
381   }
382   bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
383     return parseSectionSwitch("__DATA", "__thread_init",
384                          MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
385   }
386   bool parseVersionMin(StringRef, SMLoc);
387
388 };
389
390 } // end anonymous namespace
391
392 bool DarwinAsmParser::parseSectionSwitch(const char *Segment,
393                                          const char *Section,
394                                          unsigned TAA, unsigned Align,
395                                          unsigned StubSize) {
396   if (getLexer().isNot(AsmToken::EndOfStatement))
397     return TokError("unexpected token in section switching directive");
398   Lex();
399
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()));
405
406   // Set the implicit alignment, if any.
407   //
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.
414   if (Align)
415     getStreamer().EmitValueToAlignment(Align);
416
417   return false;
418 }
419
420 /// parseDirectiveAltEntry
421 ///  ::= .alt_entry identifier
422 bool DarwinAsmParser::parseDirectiveAltEntry(StringRef, SMLoc) {
423   StringRef Name;
424   if (getParser().parseIdentifier(Name))
425     return TokError("expected identifier in directive");
426
427   // Look up symbol.
428   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
429
430   if (Sym->isDefined())
431     return TokError(".alt_entry must preceed symbol definition");
432
433   if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_AltEntry))
434     return TokError("unable to emit symbol attribute");
435
436   Lex();
437   return false;
438 }
439
440 /// parseDirectiveDesc
441 ///  ::= .desc identifier , expression
442 bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
443   StringRef Name;
444   if (getParser().parseIdentifier(Name))
445     return TokError("expected identifier in directive");
446
447   // Handle the identifier as the key symbol.
448   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
449
450   if (getLexer().isNot(AsmToken::Comma))
451     return TokError("unexpected token in '.desc' directive");
452   Lex();
453
454   int64_t DescValue;
455   if (getParser().parseAbsoluteExpression(DescValue))
456     return true;
457
458   if (getLexer().isNot(AsmToken::EndOfStatement))
459     return TokError("unexpected token in '.desc' directive");
460
461   Lex();
462
463   // Set the n_desc field of this Symbol to this DescValue
464   getStreamer().EmitSymbolDesc(Sym, DescValue);
465
466   return false;
467 }
468
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 "
480                       "section");
481
482   StringRef Name;
483   if (getParser().parseIdentifier(Name))
484     return TokError("expected identifier in .indirect_symbol directive");
485
486   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
487
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");
491
492   if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
493     return TokError("unable to emit indirect symbol attribute for: " + Name);
494
495   if (getLexer().isNot(AsmToken::EndOfStatement))
496     return TokError("unexpected token in '.indirect_symbol' directive");
497
498   Lex();
499
500   return false;
501 }
502
503 /// parseDirectiveDumpOrLoad
504 ///  ::= ( .dump | .load ) "filename"
505 bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
506                                                SMLoc IDLoc) {
507   bool IsDump = Directive == ".dump";
508   if (getLexer().isNot(AsmToken::String))
509     return TokError("expected string in '.dump' or '.load' directive");
510
511   Lex();
512
513   if (getLexer().isNot(AsmToken::EndOfStatement))
514     return TokError("unexpected token in '.dump' or '.load' directive");
515
516   Lex();
517
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.
520   if (IsDump)
521     return Warning(IDLoc, "ignoring directive .dump for now");
522   else
523     return Warning(IDLoc, "ignoring directive .load for now");
524 }
525
526 /// ParseDirectiveLinkerOption
527 ///  ::= .linker_option "string" ( , "string" )*
528 bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
529   SmallVector<std::string, 4> Args;
530   for (;;) {
531     if (getLexer().isNot(AsmToken::String))
532       return TokError("expected string in '" + Twine(IDVal) + "' directive");
533
534     std::string Data;
535     if (getParser().parseEscapedString(Data))
536       return true;
537
538     Args.push_back(Data);
539
540     if (getLexer().is(AsmToken::EndOfStatement))
541       break;
542
543     if (getLexer().isNot(AsmToken::Comma))
544       return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
545     Lex();
546   }
547
548   getStreamer().EmitLinkerOptions(Args);
549   return false;
550 }
551
552 /// parseDirectiveLsym
553 ///  ::= .lsym identifier , expression
554 bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
555   StringRef Name;
556   if (getParser().parseIdentifier(Name))
557     return TokError("expected identifier in directive");
558
559   // Handle the identifier as the key symbol.
560   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
561
562   if (getLexer().isNot(AsmToken::Comma))
563     return TokError("unexpected token in '.lsym' directive");
564   Lex();
565
566   const MCExpr *Value;
567   if (getParser().parseExpression(Value))
568     return true;
569
570   if (getLexer().isNot(AsmToken::EndOfStatement))
571     return TokError("unexpected token in '.lsym' directive");
572
573   Lex();
574
575   // We don't currently support this directive.
576   //
577   // FIXME: Diagnostic location!
578   (void) Sym;
579   return TokError("directive '.lsym' is unsupported");
580 }
581
582 /// parseDirectiveSection:
583 ///   ::= .section identifier (',' identifier)*
584 bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
585   SMLoc Loc = getLexer().getLoc();
586
587   StringRef SectionName;
588   if (getParser().parseIdentifier(SectionName))
589     return Error(Loc, "expected identifier after '.section' directive");
590
591   // Verify there is a following comma.
592   if (!getLexer().is(AsmToken::Comma))
593     return TokError("unexpected token in '.section' directive");
594
595   std::string SectionSpec = SectionName;
596   SectionSpec += ",";
597
598   // Add all the tokens until the end of the line, ParseSectionSpecifier will
599   // handle this.
600   StringRef EOL = getLexer().LexUntilEndOfStatement();
601   SectionSpec.append(EOL.begin(), EOL.end());
602
603   Lex();
604   if (getLexer().isNot(AsmToken::EndOfStatement))
605     return TokError("unexpected token in '.section' directive");
606   Lex();
607
608
609   StringRef Segment, Section;
610   unsigned StubSize;
611   unsigned TAA;
612   bool TAAParsed;
613   std::string ErrorStr =
614     MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
615                                           TAA, TAAParsed, StubSize);
616
617   if (!ErrorStr.empty())
618     return Error(Loc, ErrorStr.c_str());
619
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();
623
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")
629                                    .Default(Section);
630
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));
640     }
641   }
642
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()));
648   return false;
649 }
650
651 /// ParseDirectivePushSection:
652 ///   ::= .pushsection identifier (',' identifier)*
653 bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
654   getStreamer().PushSection();
655
656   if (parseDirectiveSection(S, Loc)) {
657     getStreamer().PopSection();
658     return true;
659   }
660
661   return false;
662 }
663
664 /// ParseDirectivePopSection:
665 ///   ::= .popsection
666 bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
667   if (!getStreamer().PopSection())
668     return TokError(".popsection without corresponding .pushsection");
669   return false;
670 }
671
672 /// ParseDirectivePrevious:
673 ///   ::= .previous
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);
679   return false;
680 }
681
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");
688
689   if (getContext().getSecureLogUsed())
690     return Error(IDLoc, ".secure_log_unique specified multiple times");
691
692   // Get the secure log path.
693   const char *SecureLogFile = getContext().getSecureLogFile();
694   if (!SecureLogFile)
695     return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
696                  "environment variable unset.");
697
698   // Open the secure log file if we haven't already.
699   raw_fd_ostream *OS = getContext().getSecureLog();
700   if (!OS) {
701     std::error_code EC;
702     auto NewOS = llvm::make_unique<raw_fd_ostream>(
703         SecureLogFile, EC, sys::fs::F_Append | sys::fs::F_Text);
704     if (EC)
705        return Error(IDLoc, Twine("can't open secure log file: ") +
706                                SecureLogFile + " (" + EC.message() + ")");
707     OS = NewOS.get();
708     getContext().setSecureLog(std::move(NewOS));
709   }
710
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";
716
717   getContext().setSecureLogUsed(true);
718
719   return false;
720 }
721
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");
727
728   Lex();
729
730   getContext().setSecureLogUsed(false);
731
732   return false;
733 }
734
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");
740
741   Lex();
742
743   getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
744
745   return false;
746 }
747
748 /// ParseDirectiveTBSS
749 ///  ::= .tbss identifier, size, align
750 bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
751   SMLoc IDLoc = getLexer().getLoc();
752   StringRef Name;
753   if (getParser().parseIdentifier(Name))
754     return TokError("expected identifier in directive");
755
756   // Handle the identifier as the key symbol.
757   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
758
759   if (getLexer().isNot(AsmToken::Comma))
760     return TokError("unexpected token in directive");
761   Lex();
762
763   int64_t Size;
764   SMLoc SizeLoc = getLexer().getLoc();
765   if (getParser().parseAbsoluteExpression(Size))
766     return true;
767
768   int64_t Pow2Alignment = 0;
769   SMLoc Pow2AlignmentLoc;
770   if (getLexer().is(AsmToken::Comma)) {
771     Lex();
772     Pow2AlignmentLoc = getLexer().getLoc();
773     if (getParser().parseAbsoluteExpression(Pow2Alignment))
774       return true;
775   }
776
777   if (getLexer().isNot(AsmToken::EndOfStatement))
778     return TokError("unexpected token in '.tbss' directive");
779
780   Lex();
781
782   if (Size < 0)
783     return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
784                  "zero");
785
786   // FIXME: Diagnose overflow.
787   if (Pow2Alignment < 0)
788     return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
789                  "than zero");
790
791   if (!Sym->isUndefined())
792     return Error(IDLoc, "invalid symbol redefinition");
793
794   getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
795                                  "__DATA", "__thread_bss",
796                                  MachO::S_THREAD_LOCAL_ZEROFILL,
797                                  0, SectionKind::getThreadBSS()),
798                                Sym, Size, 1 << Pow2Alignment);
799
800   return false;
801 }
802
803 /// ParseDirectiveZerofill
804 ///  ::= .zerofill segname , sectname [, identifier , size_expression [
805 ///      , align_expression ]]
806 bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
807   StringRef Segment;
808   if (getParser().parseIdentifier(Segment))
809     return TokError("expected segment name after '.zerofill' directive");
810
811   if (getLexer().isNot(AsmToken::Comma))
812     return TokError("unexpected token in directive");
813   Lex();
814
815   StringRef Section;
816   if (getParser().parseIdentifier(Section))
817     return TokError("expected section name after comma in '.zerofill' "
818                     "directive");
819
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()));
827     return false;
828   }
829
830   if (getLexer().isNot(AsmToken::Comma))
831     return TokError("unexpected token in directive");
832   Lex();
833
834   SMLoc IDLoc = getLexer().getLoc();
835   StringRef IDStr;
836   if (getParser().parseIdentifier(IDStr))
837     return TokError("expected identifier in directive");
838
839   // handle the identifier as the key symbol.
840   MCSymbol *Sym = getContext().getOrCreateSymbol(IDStr);
841
842   if (getLexer().isNot(AsmToken::Comma))
843     return TokError("unexpected token in directive");
844   Lex();
845
846   int64_t Size;
847   SMLoc SizeLoc = getLexer().getLoc();
848   if (getParser().parseAbsoluteExpression(Size))
849     return true;
850
851   int64_t Pow2Alignment = 0;
852   SMLoc Pow2AlignmentLoc;
853   if (getLexer().is(AsmToken::Comma)) {
854     Lex();
855     Pow2AlignmentLoc = getLexer().getLoc();
856     if (getParser().parseAbsoluteExpression(Pow2Alignment))
857       return true;
858   }
859
860   if (getLexer().isNot(AsmToken::EndOfStatement))
861     return TokError("unexpected token in '.zerofill' directive");
862
863   Lex();
864
865   if (Size < 0)
866     return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
867                  "than zero");
868
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");
875
876   if (!Sym->isUndefined())
877     return Error(IDLoc, "invalid symbol redefinition");
878
879   // Create the zerofill Symbol with Size and Pow2Alignment
880   //
881   // FIXME: Arch specific.
882   getStreamer().EmitZerofill(getContext().getMachOSection(
883                                Segment, Section, MachO::S_ZEROFILL,
884                                0, SectionKind::getBSS()),
885                              Sym, Size, 1 << Pow2Alignment);
886
887   return false;
888 }
889
890 /// ParseDirectiveDataRegion
891 ///  ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
892 bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
893   if (getLexer().is(AsmToken::EndOfStatement)) {
894     Lex();
895     getStreamer().EmitDataRegion(MCDR_DataRegion);
896     return false;
897   }
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)
906     .Default(-1);
907   if (Kind == -1)
908     return Error(Loc, "unknown region type in '.data_region' directive");
909   Lex();
910
911   getStreamer().EmitDataRegion((MCDataRegionType)Kind);
912   return false;
913 }
914
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");
920
921   Lex();
922   getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
923   return false;
924 }
925
926 /// parseVersionMin
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");
942   Lex();
943   if (getLexer().isNot(AsmToken::Comma))
944     return TokError("minor OS version number required, comma expected");
945   Lex();
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");
952   Lex();
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");
957     Lex();
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");
963     Lex();
964   }
965
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;
973   }
974   if (T.getOS() != ExpectedOS)
975     Warning(Loc, Directive + " should only be used for " +
976             Triple::getOSTypeName(ExpectedOS) + " targets");
977
978   if (LastVersionMinDirective.isValid()) {
979     Warning(Loc, "overriding previous version_min directive");
980     Note(LastVersionMinDirective, "previous definition is here");
981   }
982   LastVersionMinDirective = Loc;
983
984   // We've parsed a correct version specifier, so send it to the streamer.
985   getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
986
987   return false;
988 }
989
990 namespace llvm {
991
992 MCAsmParserExtension *createDarwinAsmParser() {
993   return new DarwinAsmParser;
994 }
995
996 } // end llvm namespace