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