MC: Move AsmParser::TokError to MCAsmParser().
[oota-llvm.git] / lib / MC / MCParser / AsmParser.cpp
1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
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 // This class implements the parser for assembly files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/MC/MCParser/AsmParser.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCSectionMachO.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Target/TargetAsmParser.h"
30 using namespace llvm;
31
32
33 enum { DEFAULT_ADDRSPACE = 0 };
34
35 AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
36                      MCStreamer &_Out, const MCAsmInfo &_MAI)
37   : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
38     CurBuffer(0) {
39   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
40   
41   // Debugging directives.
42   AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
43   AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
44   AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
45 }
46
47 AsmParser::~AsmParser() {
48 }
49
50 void AsmParser::Warning(SMLoc L, const Twine &Msg) {
51   PrintMessage(L, Msg.str(), "warning");
52 }
53
54 bool AsmParser::Error(SMLoc L, const Twine &Msg) {
55   PrintMessage(L, Msg.str(), "error");
56   return true;
57 }
58
59 void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg, 
60                              const char *Type) const {
61   SrcMgr.PrintMessage(Loc, Msg, Type);
62 }
63                   
64 bool AsmParser::EnterIncludeFile(const std::string &Filename) {
65   int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
66   if (NewBuf == -1)
67     return true;
68   
69   CurBuffer = NewBuf;
70   
71   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
72   
73   return false;
74 }
75                   
76 const AsmToken &AsmParser::Lex() {
77   const AsmToken *tok = &Lexer.Lex();
78   
79   if (tok->is(AsmToken::Eof)) {
80     // If this is the end of an included file, pop the parent file off the
81     // include stack.
82     SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
83     if (ParentIncludeLoc != SMLoc()) {
84       CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
85       Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), 
86                       ParentIncludeLoc.getPointer());
87       tok = &Lexer.Lex();
88     }
89   }
90     
91   if (tok->is(AsmToken::Error))
92     PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
93   
94   return *tok;
95 }
96
97 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
98   // Create the initial section, if requested.
99   //
100   // FIXME: Target hook & command line option for initial section.
101   if (!NoInitialTextSection)
102     Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
103                                       MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
104                                       0, SectionKind::getText()));
105
106   // Prime the lexer.
107   Lex();
108   
109   bool HadError = false;
110   
111   AsmCond StartingCondState = TheCondState;
112
113   // While we have input, parse each statement.
114   while (Lexer.isNot(AsmToken::Eof)) {
115     if (!ParseStatement()) continue;
116   
117     // We had an error, remember it and recover by skipping to the next line.
118     HadError = true;
119     EatToEndOfStatement();
120   }
121
122   if (TheCondState.TheCond != StartingCondState.TheCond ||
123       TheCondState.Ignore != StartingCondState.Ignore)
124     return TokError("unmatched .ifs or .elses");
125   
126   // Finalize the output stream if there are no errors and if the client wants
127   // us to.
128   if (!HadError && !NoFinalize)  
129     Out.Finish();
130
131   return HadError;
132 }
133
134 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
135 void AsmParser::EatToEndOfStatement() {
136   while (Lexer.isNot(AsmToken::EndOfStatement) &&
137          Lexer.isNot(AsmToken::Eof))
138     Lex();
139   
140   // Eat EOL.
141   if (Lexer.is(AsmToken::EndOfStatement))
142     Lex();
143 }
144
145
146 /// ParseParenExpr - Parse a paren expression and return it.
147 /// NOTE: This assumes the leading '(' has already been consumed.
148 ///
149 /// parenexpr ::= expr)
150 ///
151 bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
152   if (ParseExpression(Res)) return true;
153   if (Lexer.isNot(AsmToken::RParen))
154     return TokError("expected ')' in parentheses expression");
155   EndLoc = Lexer.getLoc();
156   Lex();
157   return false;
158 }
159
160 MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
161   // FIXME: Inline into callers.
162   return Ctx.GetOrCreateSymbol(Name);
163 }
164
165 /// ParsePrimaryExpr - Parse a primary expression and return it.
166 ///  primaryexpr ::= (parenexpr
167 ///  primaryexpr ::= symbol
168 ///  primaryexpr ::= number
169 ///  primaryexpr ::= '.'
170 ///  primaryexpr ::= ~,+,- primaryexpr
171 bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
172   switch (Lexer.getKind()) {
173   default:
174     return TokError("unknown token in expression");
175   case AsmToken::Exclaim:
176     Lex(); // Eat the operator.
177     if (ParsePrimaryExpr(Res, EndLoc))
178       return true;
179     Res = MCUnaryExpr::CreateLNot(Res, getContext());
180     return false;
181   case AsmToken::String:
182   case AsmToken::Identifier: {
183     // This is a symbol reference.
184     std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
185     MCSymbol *Sym = CreateSymbol(Split.first);
186
187     // Mark the symbol as used in an expression.
188     Sym->setUsedInExpr(true);
189
190     // Lookup the symbol variant if used.
191     MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
192     if (Split.first.size() != getTok().getIdentifier().size())
193       Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
194
195     EndLoc = Lexer.getLoc();
196     Lex(); // Eat identifier.
197
198     // If this is an absolute variable reference, substitute it now to preserve
199     // semantics in the face of reassignment.
200     if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
201       if (Variant)
202         return Error(EndLoc, "unexpected modified on variable reference");
203
204       Res = Sym->getVariableValue();
205       return false;
206     }
207
208     // Otherwise create a symbol ref.
209     Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
210     return false;
211   }
212   case AsmToken::Integer: {
213     SMLoc Loc = getTok().getLoc();
214     int64_t IntVal = getTok().getIntVal();
215     Res = MCConstantExpr::Create(IntVal, getContext());
216     EndLoc = Lexer.getLoc();
217     Lex(); // Eat token.
218     // Look for 'b' or 'f' following an Integer as a directional label
219     if (Lexer.getKind() == AsmToken::Identifier) {
220       StringRef IDVal = getTok().getString();
221       if (IDVal == "f" || IDVal == "b"){
222         MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
223                                                       IDVal == "f" ? 1 : 0);
224         Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
225                                       getContext());
226         if(IDVal == "b" && Sym->isUndefined())
227           return Error(Loc, "invalid reference to undefined symbol");
228         EndLoc = Lexer.getLoc();
229         Lex(); // Eat identifier.
230       }
231     }
232     return false;
233   }
234   case AsmToken::Dot: {
235     // This is a '.' reference, which references the current PC.  Emit a
236     // temporary label to the streamer and refer to it.
237     MCSymbol *Sym = Ctx.CreateTempSymbol();
238     Out.EmitLabel(Sym);
239     Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
240     EndLoc = Lexer.getLoc();
241     Lex(); // Eat identifier.
242     return false;
243   }
244       
245   case AsmToken::LParen:
246     Lex(); // Eat the '('.
247     return ParseParenExpr(Res, EndLoc);
248   case AsmToken::Minus:
249     Lex(); // Eat the operator.
250     if (ParsePrimaryExpr(Res, EndLoc))
251       return true;
252     Res = MCUnaryExpr::CreateMinus(Res, getContext());
253     return false;
254   case AsmToken::Plus:
255     Lex(); // Eat the operator.
256     if (ParsePrimaryExpr(Res, EndLoc))
257       return true;
258     Res = MCUnaryExpr::CreatePlus(Res, getContext());
259     return false;
260   case AsmToken::Tilde:
261     Lex(); // Eat the operator.
262     if (ParsePrimaryExpr(Res, EndLoc))
263       return true;
264     Res = MCUnaryExpr::CreateNot(Res, getContext());
265     return false;
266   }
267 }
268
269 bool AsmParser::ParseExpression(const MCExpr *&Res) {
270   SMLoc EndLoc;
271   return ParseExpression(Res, EndLoc);
272 }
273
274 /// ParseExpression - Parse an expression and return it.
275 /// 
276 ///  expr ::= expr +,- expr          -> lowest.
277 ///  expr ::= expr |,^,&,! expr      -> middle.
278 ///  expr ::= expr *,/,%,<<,>> expr  -> highest.
279 ///  expr ::= primaryexpr
280 ///
281 bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
282   // Parse the expression.
283   Res = 0;
284   if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
285     return true;
286
287   // Try to constant fold it up front, if possible.
288   int64_t Value;
289   if (Res->EvaluateAsAbsolute(Value))
290     Res = MCConstantExpr::Create(Value, getContext());
291
292   return false;
293 }
294
295 bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
296   Res = 0;
297   return ParseParenExpr(Res, EndLoc) ||
298          ParseBinOpRHS(1, Res, EndLoc);
299 }
300
301 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
302   const MCExpr *Expr;
303   
304   SMLoc StartLoc = Lexer.getLoc();
305   if (ParseExpression(Expr))
306     return true;
307
308   if (!Expr->EvaluateAsAbsolute(Res))
309     return Error(StartLoc, "expected absolute expression");
310
311   return false;
312 }
313
314 static unsigned getBinOpPrecedence(AsmToken::TokenKind K, 
315                                    MCBinaryExpr::Opcode &Kind) {
316   switch (K) {
317   default:
318     return 0;    // not a binop.
319
320     // Lowest Precedence: &&, ||
321   case AsmToken::AmpAmp:
322     Kind = MCBinaryExpr::LAnd;
323     return 1;
324   case AsmToken::PipePipe:
325     Kind = MCBinaryExpr::LOr;
326     return 1;
327
328     // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
329   case AsmToken::Plus:
330     Kind = MCBinaryExpr::Add;
331     return 2;
332   case AsmToken::Minus:
333     Kind = MCBinaryExpr::Sub;
334     return 2;
335   case AsmToken::EqualEqual:
336     Kind = MCBinaryExpr::EQ;
337     return 2;
338   case AsmToken::ExclaimEqual:
339   case AsmToken::LessGreater:
340     Kind = MCBinaryExpr::NE;
341     return 2;
342   case AsmToken::Less:
343     Kind = MCBinaryExpr::LT;
344     return 2;
345   case AsmToken::LessEqual:
346     Kind = MCBinaryExpr::LTE;
347     return 2;
348   case AsmToken::Greater:
349     Kind = MCBinaryExpr::GT;
350     return 2;
351   case AsmToken::GreaterEqual:
352     Kind = MCBinaryExpr::GTE;
353     return 2;
354
355     // Intermediate Precedence: |, &, ^
356     //
357     // FIXME: gas seems to support '!' as an infix operator?
358   case AsmToken::Pipe:
359     Kind = MCBinaryExpr::Or;
360     return 3;
361   case AsmToken::Caret:
362     Kind = MCBinaryExpr::Xor;
363     return 3;
364   case AsmToken::Amp:
365     Kind = MCBinaryExpr::And;
366     return 3;
367
368     // Highest Precedence: *, /, %, <<, >>
369   case AsmToken::Star:
370     Kind = MCBinaryExpr::Mul;
371     return 4;
372   case AsmToken::Slash:
373     Kind = MCBinaryExpr::Div;
374     return 4;
375   case AsmToken::Percent:
376     Kind = MCBinaryExpr::Mod;
377     return 4;
378   case AsmToken::LessLess:
379     Kind = MCBinaryExpr::Shl;
380     return 4;
381   case AsmToken::GreaterGreater:
382     Kind = MCBinaryExpr::Shr;
383     return 4;
384   }
385 }
386
387
388 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
389 /// Res contains the LHS of the expression on input.
390 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
391                               SMLoc &EndLoc) {
392   while (1) {
393     MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
394     unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
395     
396     // If the next token is lower precedence than we are allowed to eat, return
397     // successfully with what we ate already.
398     if (TokPrec < Precedence)
399       return false;
400     
401     Lex();
402     
403     // Eat the next primary expression.
404     const MCExpr *RHS;
405     if (ParsePrimaryExpr(RHS, EndLoc)) return true;
406     
407     // If BinOp binds less tightly with RHS than the operator after RHS, let
408     // the pending operator take RHS as its LHS.
409     MCBinaryExpr::Opcode Dummy;
410     unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
411     if (TokPrec < NextTokPrec) {
412       if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
413     }
414
415     // Merge LHS and RHS according to operator.
416     Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
417   }
418 }
419
420   
421   
422   
423 /// ParseStatement:
424 ///   ::= EndOfStatement
425 ///   ::= Label* Directive ...Operands... EndOfStatement
426 ///   ::= Label* Identifier OperandList* EndOfStatement
427 bool AsmParser::ParseStatement() {
428   if (Lexer.is(AsmToken::EndOfStatement)) {
429     Out.AddBlankLine();
430     Lex();
431     return false;
432   }
433
434   // Statements always start with an identifier.
435   AsmToken ID = getTok();
436   SMLoc IDLoc = ID.getLoc();
437   StringRef IDVal;
438   int64_t LocalLabelVal = -1;
439   // GUESS allow an integer followed by a ':' as a directional local label
440   if (Lexer.is(AsmToken::Integer)) {
441     LocalLabelVal = getTok().getIntVal();
442     if (LocalLabelVal < 0) {
443       if (!TheCondState.Ignore)
444         return TokError("unexpected token at start of statement");
445       IDVal = "";
446     }
447     else {
448       IDVal = getTok().getString();
449       Lex(); // Consume the integer token to be used as an identifier token.
450       if (Lexer.getKind() != AsmToken::Colon) {
451         if (!TheCondState.Ignore)
452           return TokError("unexpected token at start of statement");
453       }
454     }
455   }
456   else if (ParseIdentifier(IDVal)) {
457     if (!TheCondState.Ignore)
458       return TokError("unexpected token at start of statement");
459     IDVal = "";
460   }
461
462   // Handle conditional assembly here before checking for skipping.  We
463   // have to do this so that .endif isn't skipped in a ".if 0" block for
464   // example.
465   if (IDVal == ".if")
466     return ParseDirectiveIf(IDLoc);
467   if (IDVal == ".elseif")
468     return ParseDirectiveElseIf(IDLoc);
469   if (IDVal == ".else")
470     return ParseDirectiveElse(IDLoc);
471   if (IDVal == ".endif")
472     return ParseDirectiveEndIf(IDLoc);
473     
474   // If we are in a ".if 0" block, ignore this statement.
475   if (TheCondState.Ignore) {
476     EatToEndOfStatement();
477     return false;
478   }
479   
480   // FIXME: Recurse on local labels?
481
482   // See what kind of statement we have.
483   switch (Lexer.getKind()) {
484   case AsmToken::Colon: {
485     // identifier ':'   -> Label.
486     Lex();
487
488     // Diagnose attempt to use a variable as a label.
489     //
490     // FIXME: Diagnostics. Note the location of the definition as a label.
491     // FIXME: This doesn't diagnose assignment to a symbol which has been
492     // implicitly marked as external.
493     MCSymbol *Sym;
494     if (LocalLabelVal == -1)
495       Sym = CreateSymbol(IDVal);
496     else
497       Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
498     if (!Sym->isUndefined() || Sym->isVariable())
499       return Error(IDLoc, "invalid symbol redefinition");
500     
501     // Emit the label.
502     Out.EmitLabel(Sym);
503    
504     // Consume any end of statement token, if present, to avoid spurious
505     // AddBlankLine calls().
506     if (Lexer.is(AsmToken::EndOfStatement)) {
507       Lex();
508       if (Lexer.is(AsmToken::Eof))
509         return false;
510     }
511
512     return ParseStatement();
513   }
514
515   case AsmToken::Equal:
516     // identifier '=' ... -> assignment statement
517     Lex();
518
519     return ParseAssignment(IDVal);
520
521   default: // Normal instruction or directive.
522     break;
523   }
524   
525   // Otherwise, we have a normal instruction or directive.  
526   if (IDVal[0] == '.') {
527     // FIXME: This should be driven based on a hash lookup and callback.
528     if (IDVal == ".section")
529       return ParseDirectiveDarwinSection();
530     if (IDVal == ".text")
531       // FIXME: This changes behavior based on the -static flag to the
532       // assembler.
533       return ParseDirectiveSectionSwitch("__TEXT", "__text",
534                                      MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
535     if (IDVal == ".const")
536       return ParseDirectiveSectionSwitch("__TEXT", "__const");
537     if (IDVal == ".static_const")
538       return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
539     if (IDVal == ".cstring")
540       return ParseDirectiveSectionSwitch("__TEXT","__cstring", 
541                                          MCSectionMachO::S_CSTRING_LITERALS);
542     if (IDVal == ".literal4")
543       return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
544                                          MCSectionMachO::S_4BYTE_LITERALS,
545                                          4);
546     if (IDVal == ".literal8")
547       return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
548                                          MCSectionMachO::S_8BYTE_LITERALS,
549                                          8);
550     if (IDVal == ".literal16")
551       return ParseDirectiveSectionSwitch("__TEXT","__literal16",
552                                          MCSectionMachO::S_16BYTE_LITERALS,
553                                          16);
554     if (IDVal == ".constructor")
555       return ParseDirectiveSectionSwitch("__TEXT","__constructor");
556     if (IDVal == ".destructor")
557       return ParseDirectiveSectionSwitch("__TEXT","__destructor");
558     if (IDVal == ".fvmlib_init0")
559       return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
560     if (IDVal == ".fvmlib_init1")
561       return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
562
563     // FIXME: The assembler manual claims that this has the self modify code
564     // flag, at least on x86-32, but that does not appear to be correct.
565     if (IDVal == ".symbol_stub")
566       return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
567                                          MCSectionMachO::S_SYMBOL_STUBS |
568                                        MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
569                                           // FIXME: Different on PPC and ARM.
570                                          0, 16);
571     // FIXME: PowerPC only?
572     if (IDVal == ".picsymbol_stub")
573       return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
574                                          MCSectionMachO::S_SYMBOL_STUBS |
575                                        MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
576                                          0, 26);
577     if (IDVal == ".data")
578       return ParseDirectiveSectionSwitch("__DATA", "__data");
579     if (IDVal == ".static_data")
580       return ParseDirectiveSectionSwitch("__DATA", "__static_data");
581
582     // FIXME: The section names of these two are misspelled in the assembler
583     // manual.
584     if (IDVal == ".non_lazy_symbol_pointer")
585       return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
586                                      MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
587                                          4);
588     if (IDVal == ".lazy_symbol_pointer")
589       return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
590                                          MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
591                                          4);
592
593     if (IDVal == ".dyld")
594       return ParseDirectiveSectionSwitch("__DATA", "__dyld");
595     if (IDVal == ".mod_init_func")
596       return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
597                                        MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
598                                          4);
599     if (IDVal == ".mod_term_func")
600       return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
601                                        MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
602                                          4);
603     if (IDVal == ".const_data")
604       return ParseDirectiveSectionSwitch("__DATA", "__const");
605     
606     
607     if (IDVal == ".objc_class")
608       return ParseDirectiveSectionSwitch("__OBJC", "__class", 
609                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
610     if (IDVal == ".objc_meta_class")
611       return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
612                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
613     if (IDVal == ".objc_cat_cls_meth")
614       return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
615                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
616     if (IDVal == ".objc_cat_inst_meth")
617       return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
618                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
619     if (IDVal == ".objc_protocol")
620       return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
621                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
622     if (IDVal == ".objc_string_object")
623       return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
624                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
625     if (IDVal == ".objc_cls_meth")
626       return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
627                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
628     if (IDVal == ".objc_inst_meth")
629       return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
630                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
631     if (IDVal == ".objc_cls_refs")
632       return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
633                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
634                                          MCSectionMachO::S_LITERAL_POINTERS,
635                                          4);
636     if (IDVal == ".objc_message_refs")
637       return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
638                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
639                                          MCSectionMachO::S_LITERAL_POINTERS,
640                                          4);
641     if (IDVal == ".objc_symbols")
642       return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
643                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
644     if (IDVal == ".objc_category")
645       return ParseDirectiveSectionSwitch("__OBJC", "__category",
646                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
647     if (IDVal == ".objc_class_vars")
648       return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
649                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
650     if (IDVal == ".objc_instance_vars")
651       return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
652                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
653     if (IDVal == ".objc_module_info")
654       return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
655                                          MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
656     if (IDVal == ".objc_class_names")
657       return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
658                                          MCSectionMachO::S_CSTRING_LITERALS);
659     if (IDVal == ".objc_meth_var_types")
660       return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
661                                          MCSectionMachO::S_CSTRING_LITERALS);
662     if (IDVal == ".objc_meth_var_names")
663       return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
664                                          MCSectionMachO::S_CSTRING_LITERALS);
665     if (IDVal == ".objc_selector_strs")
666       return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
667                                          MCSectionMachO::S_CSTRING_LITERALS);
668     
669     if (IDVal == ".tdata")
670       return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
671                                         MCSectionMachO::S_THREAD_LOCAL_REGULAR);
672     if (IDVal == ".tlv")
673       return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
674                                       MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
675     if (IDVal == ".thread_init_func")
676       return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
677                         MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
678     
679     // Assembler features
680     if (IDVal == ".set")
681       return ParseDirectiveSet();
682
683     // Data directives
684
685     if (IDVal == ".ascii")
686       return ParseDirectiveAscii(false);
687     if (IDVal == ".asciz")
688       return ParseDirectiveAscii(true);
689
690     if (IDVal == ".byte")
691       return ParseDirectiveValue(1);
692     if (IDVal == ".short")
693       return ParseDirectiveValue(2);
694     if (IDVal == ".long")
695       return ParseDirectiveValue(4);
696     if (IDVal == ".quad")
697       return ParseDirectiveValue(8);
698
699     // FIXME: Target hooks for IsPow2.
700     if (IDVal == ".align")
701       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
702     if (IDVal == ".align32")
703       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
704     if (IDVal == ".balign")
705       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
706     if (IDVal == ".balignw")
707       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
708     if (IDVal == ".balignl")
709       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
710     if (IDVal == ".p2align")
711       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
712     if (IDVal == ".p2alignw")
713       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
714     if (IDVal == ".p2alignl")
715       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
716
717     if (IDVal == ".org")
718       return ParseDirectiveOrg();
719
720     if (IDVal == ".fill")
721       return ParseDirectiveFill();
722     if (IDVal == ".space")
723       return ParseDirectiveSpace();
724
725     // Symbol attribute directives
726
727     if (IDVal == ".globl" || IDVal == ".global")
728       return ParseDirectiveSymbolAttribute(MCSA_Global);
729     if (IDVal == ".hidden")
730       return ParseDirectiveSymbolAttribute(MCSA_Hidden);
731     if (IDVal == ".indirect_symbol")
732       return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
733     if (IDVal == ".internal")
734       return ParseDirectiveSymbolAttribute(MCSA_Internal);
735     if (IDVal == ".lazy_reference")
736       return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
737     if (IDVal == ".no_dead_strip")
738       return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
739     if (IDVal == ".private_extern")
740       return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
741     if (IDVal == ".protected")
742       return ParseDirectiveSymbolAttribute(MCSA_Protected);
743     if (IDVal == ".reference")
744       return ParseDirectiveSymbolAttribute(MCSA_Reference);
745     if (IDVal == ".type")
746       return ParseDirectiveELFType();
747     if (IDVal == ".weak")
748       return ParseDirectiveSymbolAttribute(MCSA_Weak);
749     if (IDVal == ".weak_definition")
750       return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
751     if (IDVal == ".weak_reference")
752       return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
753     if (IDVal == ".weak_def_can_be_hidden")
754       return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
755
756     if (IDVal == ".comm")
757       return ParseDirectiveComm(/*IsLocal=*/false);
758     if (IDVal == ".lcomm")
759       return ParseDirectiveComm(/*IsLocal=*/true);
760     if (IDVal == ".zerofill")
761       return ParseDirectiveDarwinZerofill();
762     if (IDVal == ".desc")
763       return ParseDirectiveDarwinSymbolDesc();
764     if (IDVal == ".lsym")
765       return ParseDirectiveDarwinLsym();
766     if (IDVal == ".tbss")
767       return ParseDirectiveDarwinTBSS();
768
769     if (IDVal == ".subsections_via_symbols")
770       return ParseDirectiveDarwinSubsectionsViaSymbols();
771     if (IDVal == ".abort")
772       return ParseDirectiveAbort();
773     if (IDVal == ".include")
774       return ParseDirectiveInclude();
775     if (IDVal == ".dump")
776       return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
777     if (IDVal == ".load")
778       return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
779     if (IDVal == ".secure_log_unique")
780       return ParseDirectiveDarwinSecureLogUnique(IDLoc);
781     if (IDVal == ".secure_log_reset")
782       return ParseDirectiveDarwinSecureLogReset(IDLoc);
783
784     // Look up the handler in the handler table, 
785     bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
786     if (Handler)
787       return (this->*Handler)(IDVal, IDLoc);
788     
789     // Target hook for parsing target specific directives.
790     if (!getTargetParser().ParseDirective(ID))
791       return false;
792
793     Warning(IDLoc, "ignoring directive for now");
794     EatToEndOfStatement();
795     return false;
796   }
797
798   // Canonicalize the opcode to lower case.
799   SmallString<128> Opcode;
800   for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
801     Opcode.push_back(tolower(IDVal[i]));
802   
803   SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
804   bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
805                                                      ParsedOperands);
806   if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
807     HadError = TokError("unexpected token in argument list");
808
809   // If parsing succeeded, match the instruction.
810   if (!HadError) {
811     MCInst Inst;
812     if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
813       // Emit the instruction on success.
814       Out.EmitInstruction(Inst);
815     } else {
816       // Otherwise emit a diagnostic about the match failure and set the error
817       // flag.
818       //
819       // FIXME: We should give nicer diagnostics about the exact failure.
820       Error(IDLoc, "unrecognized instruction");
821       HadError = true;
822     }
823   }
824
825   // If there was no error, consume the end-of-statement token. Otherwise this
826   // will be done by our caller.
827   if (!HadError)
828     Lex();
829
830   // Free any parsed operands.
831   for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
832     delete ParsedOperands[i];
833
834   return HadError;
835 }
836
837 bool AsmParser::ParseAssignment(const StringRef &Name) {
838   // FIXME: Use better location, we should use proper tokens.
839   SMLoc EqualLoc = Lexer.getLoc();
840
841   const MCExpr *Value;
842   if (ParseExpression(Value))
843     return true;
844   
845   if (Lexer.isNot(AsmToken::EndOfStatement))
846     return TokError("unexpected token in assignment");
847
848   // Eat the end of statement marker.
849   Lex();
850
851   // Validate that the LHS is allowed to be a variable (either it has not been
852   // used as a symbol, or it is an absolute symbol).
853   MCSymbol *Sym = getContext().LookupSymbol(Name);
854   if (Sym) {
855     // Diagnose assignment to a label.
856     //
857     // FIXME: Diagnostics. Note the location of the definition as a label.
858     // FIXME: Diagnose assignment to protected identifier (e.g., register name).
859     if (Sym->isUndefined() && !Sym->isUsedInExpr())
860       ; // Allow redefinitions of undefined symbols only used in directives.
861     else if (!Sym->isUndefined() && !Sym->isAbsolute())
862       return Error(EqualLoc, "redefinition of '" + Name + "'");
863     else if (!Sym->isVariable())
864       return Error(EqualLoc, "invalid assignment to '" + Name + "'");
865     else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
866       return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
867                    Name + "'");
868   } else
869     Sym = CreateSymbol(Name);
870
871   // FIXME: Handle '.'.
872
873   Sym->setUsedInExpr(true);
874
875   // Do the assignment.
876   Out.EmitAssignment(Sym, Value);
877
878   return false;
879 }
880
881 /// ParseIdentifier:
882 ///   ::= identifier
883 ///   ::= string
884 bool AsmParser::ParseIdentifier(StringRef &Res) {
885   if (Lexer.isNot(AsmToken::Identifier) &&
886       Lexer.isNot(AsmToken::String))
887     return true;
888
889   Res = getTok().getIdentifier();
890
891   Lex(); // Consume the identifier token.
892
893   return false;
894 }
895
896 /// ParseDirectiveSet:
897 ///   ::= .set identifier ',' expression
898 bool AsmParser::ParseDirectiveSet() {
899   StringRef Name;
900
901   if (ParseIdentifier(Name))
902     return TokError("expected identifier after '.set' directive");
903   
904   if (Lexer.isNot(AsmToken::Comma))
905     return TokError("unexpected token in '.set'");
906   Lex();
907
908   return ParseAssignment(Name);
909 }
910
911 /// ParseDirectiveSection:
912 ///   ::= .section identifier (',' identifier)*
913 /// FIXME: This should actually parse out the segment, section, attributes and
914 /// sizeof_stub fields.
915 bool AsmParser::ParseDirectiveDarwinSection() {
916   SMLoc Loc = Lexer.getLoc();
917
918   StringRef SectionName;
919   if (ParseIdentifier(SectionName))
920     return Error(Loc, "expected identifier after '.section' directive");
921
922   // Verify there is a following comma.
923   if (!Lexer.is(AsmToken::Comma))
924     return TokError("unexpected token in '.section' directive");
925
926   std::string SectionSpec = SectionName;
927   SectionSpec += ",";
928
929   // Add all the tokens until the end of the line, ParseSectionSpecifier will
930   // handle this.
931   StringRef EOL = Lexer.LexUntilEndOfStatement();
932   SectionSpec.append(EOL.begin(), EOL.end());
933
934   Lex();
935   if (Lexer.isNot(AsmToken::EndOfStatement))
936     return TokError("unexpected token in '.section' directive");
937   Lex();
938
939
940   StringRef Segment, Section;
941   unsigned TAA, StubSize;
942   std::string ErrorStr = 
943     MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
944                                           TAA, StubSize);
945   
946   if (!ErrorStr.empty())
947     return Error(Loc, ErrorStr.c_str());
948   
949   // FIXME: Arch specific.
950   bool isText = Segment == "__TEXT";  // FIXME: Hack.
951   Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
952                                         isText ? SectionKind::getText()
953                                                : SectionKind::getDataRel()));
954   return false;
955 }
956
957 /// ParseDirectiveSectionSwitch - 
958 bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
959                                             const char *Section,
960                                             unsigned TAA, unsigned Align,
961                                             unsigned StubSize) {
962   if (Lexer.isNot(AsmToken::EndOfStatement))
963     return TokError("unexpected token in section switching directive");
964   Lex();
965   
966   // FIXME: Arch specific.
967   bool isText = StringRef(Segment) == "__TEXT";  // FIXME: Hack.
968   Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
969                                         isText ? SectionKind::getText()
970                                                : SectionKind::getDataRel()));
971
972   // Set the implicit alignment, if any.
973   //
974   // FIXME: This isn't really what 'as' does; I think it just uses the implicit
975   // alignment on the section (e.g., if one manually inserts bytes into the
976   // section, then just issueing the section switch directive will not realign
977   // the section. However, this is arguably more reasonable behavior, and there
978   // is no good reason for someone to intentionally emit incorrectly sized
979   // values into the implicitly aligned sections.
980   if (Align)
981     Out.EmitValueToAlignment(Align, 0, 1, 0);
982
983   return false;
984 }
985
986 bool AsmParser::ParseEscapedString(std::string &Data) {
987   assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
988
989   Data = "";
990   StringRef Str = getTok().getStringContents();
991   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
992     if (Str[i] != '\\') {
993       Data += Str[i];
994       continue;
995     }
996
997     // Recognize escaped characters. Note that this escape semantics currently
998     // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
999     ++i;
1000     if (i == e)
1001       return TokError("unexpected backslash at end of string");
1002
1003     // Recognize octal sequences.
1004     if ((unsigned) (Str[i] - '0') <= 7) {
1005       // Consume up to three octal characters.
1006       unsigned Value = Str[i] - '0';
1007
1008       if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1009         ++i;
1010         Value = Value * 8 + (Str[i] - '0');
1011
1012         if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1013           ++i;
1014           Value = Value * 8 + (Str[i] - '0');
1015         }
1016       }
1017
1018       if (Value > 255)
1019         return TokError("invalid octal escape sequence (out of range)");
1020
1021       Data += (unsigned char) Value;
1022       continue;
1023     }
1024
1025     // Otherwise recognize individual escapes.
1026     switch (Str[i]) {
1027     default:
1028       // Just reject invalid escape sequences for now.
1029       return TokError("invalid escape sequence (unrecognized character)");
1030
1031     case 'b': Data += '\b'; break;
1032     case 'f': Data += '\f'; break;
1033     case 'n': Data += '\n'; break;
1034     case 'r': Data += '\r'; break;
1035     case 't': Data += '\t'; break;
1036     case '"': Data += '"'; break;
1037     case '\\': Data += '\\'; break;
1038     }
1039   }
1040
1041   return false;
1042 }
1043
1044 /// ParseDirectiveAscii:
1045 ///   ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
1046 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
1047   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1048     for (;;) {
1049       if (Lexer.isNot(AsmToken::String))
1050         return TokError("expected string in '.ascii' or '.asciz' directive");
1051       
1052       std::string Data;
1053       if (ParseEscapedString(Data))
1054         return true;
1055       
1056       Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
1057       if (ZeroTerminated)
1058         Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1059       
1060       Lex();
1061       
1062       if (Lexer.is(AsmToken::EndOfStatement))
1063         break;
1064
1065       if (Lexer.isNot(AsmToken::Comma))
1066         return TokError("unexpected token in '.ascii' or '.asciz' directive");
1067       Lex();
1068     }
1069   }
1070
1071   Lex();
1072   return false;
1073 }
1074
1075 /// ParseDirectiveValue
1076 ///  ::= (.byte | .short | ... ) [ expression (, expression)* ]
1077 bool AsmParser::ParseDirectiveValue(unsigned Size) {
1078   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1079     for (;;) {
1080       const MCExpr *Value;
1081       SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
1082       if (ParseExpression(Value))
1083         return true;
1084
1085       // Special case constant expressions to match code generator.
1086       if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
1087         Out.EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
1088       else
1089         Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
1090
1091       if (Lexer.is(AsmToken::EndOfStatement))
1092         break;
1093       
1094       // FIXME: Improve diagnostic.
1095       if (Lexer.isNot(AsmToken::Comma))
1096         return TokError("unexpected token in directive");
1097       Lex();
1098     }
1099   }
1100
1101   Lex();
1102   return false;
1103 }
1104
1105 /// ParseDirectiveSpace
1106 ///  ::= .space expression [ , expression ]
1107 bool AsmParser::ParseDirectiveSpace() {
1108   int64_t NumBytes;
1109   if (ParseAbsoluteExpression(NumBytes))
1110     return true;
1111
1112   int64_t FillExpr = 0;
1113   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1114     if (Lexer.isNot(AsmToken::Comma))
1115       return TokError("unexpected token in '.space' directive");
1116     Lex();
1117     
1118     if (ParseAbsoluteExpression(FillExpr))
1119       return true;
1120
1121     if (Lexer.isNot(AsmToken::EndOfStatement))
1122       return TokError("unexpected token in '.space' directive");
1123   }
1124
1125   Lex();
1126
1127   if (NumBytes <= 0)
1128     return TokError("invalid number of bytes in '.space' directive");
1129
1130   // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1131   Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
1132
1133   return false;
1134 }
1135
1136 /// ParseDirectiveFill
1137 ///  ::= .fill expression , expression , expression
1138 bool AsmParser::ParseDirectiveFill() {
1139   int64_t NumValues;
1140   if (ParseAbsoluteExpression(NumValues))
1141     return true;
1142
1143   if (Lexer.isNot(AsmToken::Comma))
1144     return TokError("unexpected token in '.fill' directive");
1145   Lex();
1146   
1147   int64_t FillSize;
1148   if (ParseAbsoluteExpression(FillSize))
1149     return true;
1150
1151   if (Lexer.isNot(AsmToken::Comma))
1152     return TokError("unexpected token in '.fill' directive");
1153   Lex();
1154   
1155   int64_t FillExpr;
1156   if (ParseAbsoluteExpression(FillExpr))
1157     return true;
1158
1159   if (Lexer.isNot(AsmToken::EndOfStatement))
1160     return TokError("unexpected token in '.fill' directive");
1161   
1162   Lex();
1163
1164   if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1165     return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1166
1167   for (uint64_t i = 0, e = NumValues; i != e; ++i)
1168     Out.EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
1169
1170   return false;
1171 }
1172
1173 /// ParseDirectiveOrg
1174 ///  ::= .org expression [ , expression ]
1175 bool AsmParser::ParseDirectiveOrg() {
1176   const MCExpr *Offset;
1177   if (ParseExpression(Offset))
1178     return true;
1179
1180   // Parse optional fill expression.
1181   int64_t FillExpr = 0;
1182   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1183     if (Lexer.isNot(AsmToken::Comma))
1184       return TokError("unexpected token in '.org' directive");
1185     Lex();
1186     
1187     if (ParseAbsoluteExpression(FillExpr))
1188       return true;
1189
1190     if (Lexer.isNot(AsmToken::EndOfStatement))
1191       return TokError("unexpected token in '.org' directive");
1192   }
1193
1194   Lex();
1195
1196   // FIXME: Only limited forms of relocatable expressions are accepted here, it
1197   // has to be relative to the current section.
1198   Out.EmitValueToOffset(Offset, FillExpr);
1199
1200   return false;
1201 }
1202
1203 /// ParseDirectiveAlign
1204 ///  ::= {.align, ...} expression [ , expression [ , expression ]]
1205 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1206   SMLoc AlignmentLoc = Lexer.getLoc();
1207   int64_t Alignment;
1208   if (ParseAbsoluteExpression(Alignment))
1209     return true;
1210
1211   SMLoc MaxBytesLoc;
1212   bool HasFillExpr = false;
1213   int64_t FillExpr = 0;
1214   int64_t MaxBytesToFill = 0;
1215   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1216     if (Lexer.isNot(AsmToken::Comma))
1217       return TokError("unexpected token in directive");
1218     Lex();
1219
1220     // The fill expression can be omitted while specifying a maximum number of
1221     // alignment bytes, e.g:
1222     //  .align 3,,4
1223     if (Lexer.isNot(AsmToken::Comma)) {
1224       HasFillExpr = true;
1225       if (ParseAbsoluteExpression(FillExpr))
1226         return true;
1227     }
1228
1229     if (Lexer.isNot(AsmToken::EndOfStatement)) {
1230       if (Lexer.isNot(AsmToken::Comma))
1231         return TokError("unexpected token in directive");
1232       Lex();
1233
1234       MaxBytesLoc = Lexer.getLoc();
1235       if (ParseAbsoluteExpression(MaxBytesToFill))
1236         return true;
1237       
1238       if (Lexer.isNot(AsmToken::EndOfStatement))
1239         return TokError("unexpected token in directive");
1240     }
1241   }
1242
1243   Lex();
1244
1245   if (!HasFillExpr)
1246     FillExpr = 0;
1247
1248   // Compute alignment in bytes.
1249   if (IsPow2) {
1250     // FIXME: Diagnose overflow.
1251     if (Alignment >= 32) {
1252       Error(AlignmentLoc, "invalid alignment value");
1253       Alignment = 31;
1254     }
1255
1256     Alignment = 1ULL << Alignment;
1257   }
1258
1259   // Diagnose non-sensical max bytes to align.
1260   if (MaxBytesLoc.isValid()) {
1261     if (MaxBytesToFill < 1) {
1262       Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1263             "many bytes, ignoring maximum bytes expression");
1264       MaxBytesToFill = 0;
1265     }
1266
1267     if (MaxBytesToFill >= Alignment) {
1268       Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1269               "has no effect");
1270       MaxBytesToFill = 0;
1271     }
1272   }
1273
1274   // Check whether we should use optimal code alignment for this .align
1275   // directive.
1276   //
1277   // FIXME: This should be using a target hook.
1278   bool UseCodeAlign = false;
1279   if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1280         Out.getCurrentSection()))
1281       UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1282   if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1283       ValueSize == 1 && UseCodeAlign) {
1284     Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
1285   } else {
1286     // FIXME: Target specific behavior about how the "extra" bytes are filled.
1287     Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1288   }
1289
1290   return false;
1291 }
1292
1293 /// ParseDirectiveSymbolAttribute
1294 ///  ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1295 bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
1296   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1297     for (;;) {
1298       StringRef Name;
1299
1300       if (ParseIdentifier(Name))
1301         return TokError("expected identifier in directive");
1302       
1303       MCSymbol *Sym = CreateSymbol(Name);
1304
1305       Out.EmitSymbolAttribute(Sym, Attr);
1306
1307       if (Lexer.is(AsmToken::EndOfStatement))
1308         break;
1309
1310       if (Lexer.isNot(AsmToken::Comma))
1311         return TokError("unexpected token in directive");
1312       Lex();
1313     }
1314   }
1315
1316   Lex();
1317   return false;  
1318 }
1319
1320 /// ParseDirectiveELFType
1321 ///  ::= .type identifier , @attribute
1322 bool AsmParser::ParseDirectiveELFType() {
1323   StringRef Name;
1324   if (ParseIdentifier(Name))
1325     return TokError("expected identifier in directive");
1326
1327   // Handle the identifier as the key symbol.
1328   MCSymbol *Sym = CreateSymbol(Name);
1329
1330   if (Lexer.isNot(AsmToken::Comma))
1331     return TokError("unexpected token in '.type' directive");
1332   Lex();
1333
1334   if (Lexer.isNot(AsmToken::At))
1335     return TokError("expected '@' before type");
1336   Lex();
1337
1338   StringRef Type;
1339   SMLoc TypeLoc;
1340
1341   TypeLoc = Lexer.getLoc();
1342   if (ParseIdentifier(Type))
1343     return TokError("expected symbol type in directive");
1344
1345   MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1346     .Case("function", MCSA_ELF_TypeFunction)
1347     .Case("object", MCSA_ELF_TypeObject)
1348     .Case("tls_object", MCSA_ELF_TypeTLS)
1349     .Case("common", MCSA_ELF_TypeCommon)
1350     .Case("notype", MCSA_ELF_TypeNoType)
1351     .Default(MCSA_Invalid);
1352
1353   if (Attr == MCSA_Invalid)
1354     return Error(TypeLoc, "unsupported attribute in '.type' directive");
1355
1356   if (Lexer.isNot(AsmToken::EndOfStatement))
1357     return TokError("unexpected token in '.type' directive");
1358
1359   Lex();
1360
1361   Out.EmitSymbolAttribute(Sym, Attr);
1362
1363   return false;
1364 }
1365
1366 /// ParseDirectiveDarwinSymbolDesc
1367 ///  ::= .desc identifier , expression
1368 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
1369   StringRef Name;
1370   if (ParseIdentifier(Name))
1371     return TokError("expected identifier in directive");
1372   
1373   // Handle the identifier as the key symbol.
1374   MCSymbol *Sym = CreateSymbol(Name);
1375
1376   if (Lexer.isNot(AsmToken::Comma))
1377     return TokError("unexpected token in '.desc' directive");
1378   Lex();
1379
1380   int64_t DescValue;
1381   if (ParseAbsoluteExpression(DescValue))
1382     return true;
1383
1384   if (Lexer.isNot(AsmToken::EndOfStatement))
1385     return TokError("unexpected token in '.desc' directive");
1386   
1387   Lex();
1388
1389   // Set the n_desc field of this Symbol to this DescValue
1390   Out.EmitSymbolDesc(Sym, DescValue);
1391
1392   return false;
1393 }
1394
1395 /// ParseDirectiveComm
1396 ///  ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1397 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1398   SMLoc IDLoc = Lexer.getLoc();
1399   StringRef Name;
1400   if (ParseIdentifier(Name))
1401     return TokError("expected identifier in directive");
1402   
1403   // Handle the identifier as the key symbol.
1404   MCSymbol *Sym = CreateSymbol(Name);
1405
1406   if (Lexer.isNot(AsmToken::Comma))
1407     return TokError("unexpected token in directive");
1408   Lex();
1409
1410   int64_t Size;
1411   SMLoc SizeLoc = Lexer.getLoc();
1412   if (ParseAbsoluteExpression(Size))
1413     return true;
1414
1415   int64_t Pow2Alignment = 0;
1416   SMLoc Pow2AlignmentLoc;
1417   if (Lexer.is(AsmToken::Comma)) {
1418     Lex();
1419     Pow2AlignmentLoc = Lexer.getLoc();
1420     if (ParseAbsoluteExpression(Pow2Alignment))
1421       return true;
1422     
1423     // If this target takes alignments in bytes (not log) validate and convert.
1424     if (Lexer.getMAI().getAlignmentIsInBytes()) {
1425       if (!isPowerOf2_64(Pow2Alignment))
1426         return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1427       Pow2Alignment = Log2_64(Pow2Alignment);
1428     }
1429   }
1430   
1431   if (Lexer.isNot(AsmToken::EndOfStatement))
1432     return TokError("unexpected token in '.comm' or '.lcomm' directive");
1433   
1434   Lex();
1435
1436   // NOTE: a size of zero for a .comm should create a undefined symbol
1437   // but a size of .lcomm creates a bss symbol of size zero.
1438   if (Size < 0)
1439     return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1440                  "be less than zero");
1441
1442   // NOTE: The alignment in the directive is a power of 2 value, the assembler
1443   // may internally end up wanting an alignment in bytes.
1444   // FIXME: Diagnose overflow.
1445   if (Pow2Alignment < 0)
1446     return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1447                  "alignment, can't be less than zero");
1448
1449   if (!Sym->isUndefined())
1450     return Error(IDLoc, "invalid symbol redefinition");
1451
1452   // '.lcomm' is equivalent to '.zerofill'.
1453   // Create the Symbol as a common or local common with Size and Pow2Alignment
1454   if (IsLocal) {
1455     Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1456                                          MCSectionMachO::S_ZEROFILL, 0,
1457                                          SectionKind::getBSS()),
1458                      Sym, Size, 1 << Pow2Alignment);
1459     return false;
1460   }
1461
1462   Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
1463   return false;
1464 }
1465
1466 /// ParseDirectiveDarwinZerofill
1467 ///  ::= .zerofill segname , sectname [, identifier , size_expression [
1468 ///      , align_expression ]]
1469 bool AsmParser::ParseDirectiveDarwinZerofill() {
1470   StringRef Segment;
1471   if (ParseIdentifier(Segment))
1472     return TokError("expected segment name after '.zerofill' directive");
1473
1474   if (Lexer.isNot(AsmToken::Comma))
1475     return TokError("unexpected token in directive");
1476   Lex();
1477
1478   StringRef Section;
1479   if (ParseIdentifier(Section))
1480     return TokError("expected section name after comma in '.zerofill' "
1481                     "directive");
1482
1483   // If this is the end of the line all that was wanted was to create the
1484   // the section but with no symbol.
1485   if (Lexer.is(AsmToken::EndOfStatement)) {
1486     // Create the zerofill section but no symbol
1487     Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1488                                          MCSectionMachO::S_ZEROFILL, 0,
1489                                          SectionKind::getBSS()));
1490     return false;
1491   }
1492
1493   if (Lexer.isNot(AsmToken::Comma))
1494     return TokError("unexpected token in directive");
1495   Lex();
1496
1497   SMLoc IDLoc = Lexer.getLoc();
1498   StringRef IDStr;
1499   if (ParseIdentifier(IDStr))
1500     return TokError("expected identifier in directive");
1501   
1502   // handle the identifier as the key symbol.
1503   MCSymbol *Sym = CreateSymbol(IDStr);
1504
1505   if (Lexer.isNot(AsmToken::Comma))
1506     return TokError("unexpected token in directive");
1507   Lex();
1508
1509   int64_t Size;
1510   SMLoc SizeLoc = Lexer.getLoc();
1511   if (ParseAbsoluteExpression(Size))
1512     return true;
1513
1514   int64_t Pow2Alignment = 0;
1515   SMLoc Pow2AlignmentLoc;
1516   if (Lexer.is(AsmToken::Comma)) {
1517     Lex();
1518     Pow2AlignmentLoc = Lexer.getLoc();
1519     if (ParseAbsoluteExpression(Pow2Alignment))
1520       return true;
1521   }
1522   
1523   if (Lexer.isNot(AsmToken::EndOfStatement))
1524     return TokError("unexpected token in '.zerofill' directive");
1525   
1526   Lex();
1527
1528   if (Size < 0)
1529     return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1530                  "than zero");
1531
1532   // NOTE: The alignment in the directive is a power of 2 value, the assembler
1533   // may internally end up wanting an alignment in bytes.
1534   // FIXME: Diagnose overflow.
1535   if (Pow2Alignment < 0)
1536     return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1537                  "can't be less than zero");
1538
1539   if (!Sym->isUndefined())
1540     return Error(IDLoc, "invalid symbol redefinition");
1541
1542   // Create the zerofill Symbol with Size and Pow2Alignment
1543   //
1544   // FIXME: Arch specific.
1545   Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1546                                        MCSectionMachO::S_ZEROFILL, 0,
1547                                        SectionKind::getBSS()),
1548                    Sym, Size, 1 << Pow2Alignment);
1549
1550   return false;
1551 }
1552
1553 /// ParseDirectiveDarwinTBSS
1554 ///  ::= .tbss identifier, size, align
1555 bool AsmParser::ParseDirectiveDarwinTBSS() {
1556   SMLoc IDLoc = Lexer.getLoc();
1557   StringRef Name;
1558   if (ParseIdentifier(Name))
1559     return TokError("expected identifier in directive");
1560     
1561   // Handle the identifier as the key symbol.
1562   MCSymbol *Sym = CreateSymbol(Name);
1563
1564   if (Lexer.isNot(AsmToken::Comma))
1565     return TokError("unexpected token in directive");
1566   Lex();
1567
1568   int64_t Size;
1569   SMLoc SizeLoc = Lexer.getLoc();
1570   if (ParseAbsoluteExpression(Size))
1571     return true;
1572
1573   int64_t Pow2Alignment = 0;
1574   SMLoc Pow2AlignmentLoc;
1575   if (Lexer.is(AsmToken::Comma)) {
1576     Lex();
1577     Pow2AlignmentLoc = Lexer.getLoc();
1578     if (ParseAbsoluteExpression(Pow2Alignment))
1579       return true;
1580   }
1581   
1582   if (Lexer.isNot(AsmToken::EndOfStatement))
1583     return TokError("unexpected token in '.tbss' directive");
1584   
1585   Lex();
1586
1587   if (Size < 0)
1588     return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1589                  "zero");
1590
1591   // FIXME: Diagnose overflow.
1592   if (Pow2Alignment < 0)
1593     return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1594                  "than zero");
1595
1596   if (!Sym->isUndefined())
1597     return Error(IDLoc, "invalid symbol redefinition");
1598   
1599   Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1600                                         MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1601                                         0, SectionKind::getThreadBSS()),
1602                      Sym, Size, 1 << Pow2Alignment);
1603   
1604   return false;
1605 }
1606
1607 /// ParseDirectiveDarwinSubsectionsViaSymbols
1608 ///  ::= .subsections_via_symbols
1609 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1610   if (Lexer.isNot(AsmToken::EndOfStatement))
1611     return TokError("unexpected token in '.subsections_via_symbols' directive");
1612   
1613   Lex();
1614
1615   Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
1616
1617   return false;
1618 }
1619
1620 /// ParseDirectiveAbort
1621 ///  ::= .abort [ "abort_string" ]
1622 bool AsmParser::ParseDirectiveAbort() {
1623   // FIXME: Use loc from directive.
1624   SMLoc Loc = Lexer.getLoc();
1625
1626   StringRef Str = "";
1627   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1628     if (Lexer.isNot(AsmToken::String))
1629       return TokError("expected string in '.abort' directive");
1630     
1631     Str = getTok().getString();
1632
1633     Lex();
1634   }
1635
1636   if (Lexer.isNot(AsmToken::EndOfStatement))
1637     return TokError("unexpected token in '.abort' directive");
1638   
1639   Lex();
1640
1641   // FIXME: Handle here.
1642   if (Str.empty())
1643     Error(Loc, ".abort detected. Assembly stopping.");
1644   else
1645     Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1646
1647   return false;
1648 }
1649
1650 /// ParseDirectiveLsym
1651 ///  ::= .lsym identifier , expression
1652 bool AsmParser::ParseDirectiveDarwinLsym() {
1653   StringRef Name;
1654   if (ParseIdentifier(Name))
1655     return TokError("expected identifier in directive");
1656   
1657   // Handle the identifier as the key symbol.
1658   MCSymbol *Sym = CreateSymbol(Name);
1659
1660   if (Lexer.isNot(AsmToken::Comma))
1661     return TokError("unexpected token in '.lsym' directive");
1662   Lex();
1663
1664   const MCExpr *Value;
1665   if (ParseExpression(Value))
1666     return true;
1667
1668   if (Lexer.isNot(AsmToken::EndOfStatement))
1669     return TokError("unexpected token in '.lsym' directive");
1670   
1671   Lex();
1672
1673   // We don't currently support this directive.
1674   //
1675   // FIXME: Diagnostic location!
1676   (void) Sym;
1677   return TokError("directive '.lsym' is unsupported");
1678 }
1679
1680 /// ParseDirectiveInclude
1681 ///  ::= .include "filename"
1682 bool AsmParser::ParseDirectiveInclude() {
1683   if (Lexer.isNot(AsmToken::String))
1684     return TokError("expected string in '.include' directive");
1685   
1686   std::string Filename = getTok().getString();
1687   SMLoc IncludeLoc = Lexer.getLoc();
1688   Lex();
1689
1690   if (Lexer.isNot(AsmToken::EndOfStatement))
1691     return TokError("unexpected token in '.include' directive");
1692   
1693   // Strip the quotes.
1694   Filename = Filename.substr(1, Filename.size()-2);
1695   
1696   // Attempt to switch the lexer to the included file before consuming the end
1697   // of statement to avoid losing it when we switch.
1698   if (EnterIncludeFile(Filename)) {
1699     PrintMessage(IncludeLoc,
1700                  "Could not find include file '" + Filename + "'",
1701                  "error");
1702     return true;
1703   }
1704
1705   return false;
1706 }
1707
1708 /// ParseDirectiveDarwinDumpOrLoad
1709 ///  ::= ( .dump | .load ) "filename"
1710 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1711   if (Lexer.isNot(AsmToken::String))
1712     return TokError("expected string in '.dump' or '.load' directive");
1713   
1714   Lex();
1715
1716   if (Lexer.isNot(AsmToken::EndOfStatement))
1717     return TokError("unexpected token in '.dump' or '.load' directive");
1718   
1719   Lex();
1720
1721   // FIXME: If/when .dump and .load are implemented they will be done in the
1722   // the assembly parser and not have any need for an MCStreamer API.
1723   if (IsDump)
1724     Warning(IDLoc, "ignoring directive .dump for now");
1725   else
1726     Warning(IDLoc, "ignoring directive .load for now");
1727
1728   return false;
1729 }
1730
1731 /// ParseDirectiveDarwinSecureLogUnique
1732 ///  ::= .secure_log_unique "log message"
1733 bool AsmParser::ParseDirectiveDarwinSecureLogUnique(SMLoc IDLoc) {
1734   std::string LogMessage;
1735
1736   if (Lexer.isNot(AsmToken::String))
1737     LogMessage = "";
1738   else{
1739     LogMessage = getTok().getString();
1740     Lex();
1741   }
1742
1743   if (Lexer.isNot(AsmToken::EndOfStatement))
1744     return TokError("unexpected token in '.secure_log_unique' directive");
1745   
1746   if (getContext().getSecureLogUsed() != false)
1747     return Error(IDLoc, ".secure_log_unique specified multiple times");
1748
1749   char *SecureLogFile = getContext().getSecureLogFile();
1750   if (SecureLogFile == NULL)
1751     return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1752                  "environment variable unset.");
1753
1754   raw_ostream *OS = getContext().getSecureLog();
1755   if (OS == NULL) {
1756     std::string Err;
1757     OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1758     if (!Err.empty()) {
1759        delete OS;
1760        return Error(IDLoc, Twine("can't open secure log file: ") +
1761                     SecureLogFile + " (" + Err + ")");
1762     }
1763     getContext().setSecureLog(OS);
1764   }
1765
1766   int CurBuf = SrcMgr.FindBufferContainingLoc(IDLoc);
1767   *OS << SrcMgr.getBufferInfo(CurBuf).Buffer->getBufferIdentifier() << ":"
1768       << SrcMgr.FindLineNumber(IDLoc, CurBuf) << ":"
1769       << LogMessage + "\n";
1770
1771   getContext().setSecureLogUsed(true);
1772
1773   return false;
1774 }
1775
1776 /// ParseDirectiveDarwinSecureLogReset
1777 ///  ::= .secure_log_reset
1778 bool AsmParser::ParseDirectiveDarwinSecureLogReset(SMLoc IDLoc) {
1779   if (Lexer.isNot(AsmToken::EndOfStatement))
1780     return TokError("unexpected token in '.secure_log_reset' directive");
1781   
1782   Lex();
1783
1784   getContext().setSecureLogUsed(false);
1785
1786   return false;
1787 }
1788
1789 /// ParseDirectiveIf
1790 /// ::= .if expression
1791 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1792   TheCondStack.push_back(TheCondState);
1793   TheCondState.TheCond = AsmCond::IfCond;
1794   if(TheCondState.Ignore) {
1795     EatToEndOfStatement();
1796   }
1797   else {
1798     int64_t ExprValue;
1799     if (ParseAbsoluteExpression(ExprValue))
1800       return true;
1801
1802     if (Lexer.isNot(AsmToken::EndOfStatement))
1803       return TokError("unexpected token in '.if' directive");
1804     
1805     Lex();
1806
1807     TheCondState.CondMet = ExprValue;
1808     TheCondState.Ignore = !TheCondState.CondMet;
1809   }
1810
1811   return false;
1812 }
1813
1814 /// ParseDirectiveElseIf
1815 /// ::= .elseif expression
1816 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1817   if (TheCondState.TheCond != AsmCond::IfCond &&
1818       TheCondState.TheCond != AsmCond::ElseIfCond)
1819       Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1820                           " an .elseif");
1821   TheCondState.TheCond = AsmCond::ElseIfCond;
1822
1823   bool LastIgnoreState = false;
1824   if (!TheCondStack.empty())
1825       LastIgnoreState = TheCondStack.back().Ignore;
1826   if (LastIgnoreState || TheCondState.CondMet) {
1827     TheCondState.Ignore = true;
1828     EatToEndOfStatement();
1829   }
1830   else {
1831     int64_t ExprValue;
1832     if (ParseAbsoluteExpression(ExprValue))
1833       return true;
1834
1835     if (Lexer.isNot(AsmToken::EndOfStatement))
1836       return TokError("unexpected token in '.elseif' directive");
1837     
1838     Lex();
1839     TheCondState.CondMet = ExprValue;
1840     TheCondState.Ignore = !TheCondState.CondMet;
1841   }
1842
1843   return false;
1844 }
1845
1846 /// ParseDirectiveElse
1847 /// ::= .else
1848 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1849   if (Lexer.isNot(AsmToken::EndOfStatement))
1850     return TokError("unexpected token in '.else' directive");
1851   
1852   Lex();
1853
1854   if (TheCondState.TheCond != AsmCond::IfCond &&
1855       TheCondState.TheCond != AsmCond::ElseIfCond)
1856       Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1857                           ".elseif");
1858   TheCondState.TheCond = AsmCond::ElseCond;
1859   bool LastIgnoreState = false;
1860   if (!TheCondStack.empty())
1861     LastIgnoreState = TheCondStack.back().Ignore;
1862   if (LastIgnoreState || TheCondState.CondMet)
1863     TheCondState.Ignore = true;
1864   else
1865     TheCondState.Ignore = false;
1866
1867   return false;
1868 }
1869
1870 /// ParseDirectiveEndIf
1871 /// ::= .endif
1872 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1873   if (Lexer.isNot(AsmToken::EndOfStatement))
1874     return TokError("unexpected token in '.endif' directive");
1875   
1876   Lex();
1877
1878   if ((TheCondState.TheCond == AsmCond::NoCond) ||
1879       TheCondStack.empty())
1880     Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1881                         ".else");
1882   if (!TheCondStack.empty()) {
1883     TheCondState = TheCondStack.back();
1884     TheCondStack.pop_back();
1885   }
1886
1887   return false;
1888 }
1889
1890 /// ParseDirectiveFile
1891 /// ::= .file [number] string
1892 bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
1893   // FIXME: I'm not sure what this is.
1894   int64_t FileNumber = -1;
1895   if (Lexer.is(AsmToken::Integer)) {
1896     FileNumber = getTok().getIntVal();
1897     Lex();
1898     
1899     if (FileNumber < 1)
1900       return TokError("file number less than one");
1901   }
1902
1903   if (Lexer.isNot(AsmToken::String))
1904     return TokError("unexpected token in '.file' directive");
1905   
1906   StringRef Filename = getTok().getString();
1907   Filename = Filename.substr(1, Filename.size()-2);
1908   Lex();
1909
1910   if (Lexer.isNot(AsmToken::EndOfStatement))
1911     return TokError("unexpected token in '.file' directive");
1912
1913   if (FileNumber == -1)
1914     Out.EmitFileDirective(Filename);
1915   else
1916     Out.EmitDwarfFileDirective(FileNumber, Filename);
1917   
1918   return false;
1919 }
1920
1921 /// ParseDirectiveLine
1922 /// ::= .line [number]
1923 bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
1924   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1925     if (Lexer.isNot(AsmToken::Integer))
1926       return TokError("unexpected token in '.line' directive");
1927
1928     int64_t LineNumber = getTok().getIntVal();
1929     (void) LineNumber;
1930     Lex();
1931
1932     // FIXME: Do something with the .line.
1933   }
1934
1935   if (Lexer.isNot(AsmToken::EndOfStatement))
1936     return TokError("unexpected token in '.line' directive");
1937
1938   return false;
1939 }
1940
1941
1942 /// ParseDirectiveLoc
1943 /// ::= .loc number [number [number]]
1944 bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
1945   if (Lexer.isNot(AsmToken::Integer))
1946     return TokError("unexpected token in '.loc' directive");
1947
1948   // FIXME: What are these fields?
1949   int64_t FileNumber = getTok().getIntVal();
1950   (void) FileNumber;
1951   // FIXME: Validate file.
1952
1953   Lex();
1954   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1955     if (Lexer.isNot(AsmToken::Integer))
1956       return TokError("unexpected token in '.loc' directive");
1957
1958     int64_t Param2 = getTok().getIntVal();
1959     (void) Param2;
1960     Lex();
1961
1962     if (Lexer.isNot(AsmToken::EndOfStatement)) {
1963       if (Lexer.isNot(AsmToken::Integer))
1964         return TokError("unexpected token in '.loc' directive");
1965
1966       int64_t Param3 = getTok().getIntVal();
1967       (void) Param3;
1968       Lex();
1969
1970       // FIXME: Do something with the .loc.
1971     }
1972   }
1973
1974   if (Lexer.isNot(AsmToken::EndOfStatement))
1975     return TokError("unexpected token in '.file' directive");
1976
1977   return false;
1978 }
1979