Added Mac OS X assembler style conditional assembly. I may come back and see if
[oota-llvm.git] / tools / llvm-mc / 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 "AsmParser.h"
15
16 #include "AsmExpr.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCSection.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetAsmParser.h"
26 using namespace llvm;
27
28 void AsmParser::Warning(SMLoc L, const Twine &Msg) {
29   Lexer.PrintMessage(L, Msg.str(), "warning");
30 }
31
32 bool AsmParser::Error(SMLoc L, const Twine &Msg) {
33   Lexer.PrintMessage(L, Msg.str(), "error");
34   return true;
35 }
36
37 bool AsmParser::TokError(const char *Msg) {
38   Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
39   return true;
40 }
41
42 bool AsmParser::Run() {
43   // Prime the lexer.
44   Lexer.Lex();
45   
46   bool HadError = false;
47   
48   AsmCond StartingCondState = TheCondState;
49
50   // While we have input, parse each statement.
51   while (Lexer.isNot(AsmToken::Eof)) {
52     // Handle conditional assembly here before calling ParseStatement()
53     if (Lexer.getKind() == AsmToken::Identifier) {
54       // If we have an identifier, handle it as the key symbol.
55       AsmToken ID = Lexer.getTok();
56       SMLoc IDLoc = ID.getLoc();
57       StringRef IDVal = ID.getString();
58
59       if (IDVal == ".if" ||
60           IDVal == ".elseif" ||
61           IDVal == ".else" ||
62           IDVal == ".endif") {
63         if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
64           continue;
65         HadError = true;
66         EatToEndOfStatement();
67         continue;
68       }
69     }
70     if (TheCondState.Ignore) {
71       EatToEndOfStatement();
72       continue;
73     }
74
75     if (!ParseStatement()) continue;
76   
77     // We had an error, remember it and recover by skipping to the next line.
78     HadError = true;
79     EatToEndOfStatement();
80   }
81
82   if (TheCondState.TheCond != StartingCondState.TheCond ||
83       TheCondState.Ignore != StartingCondState.Ignore)
84     return TokError("unmatched .ifs or .elses");
85   
86   return HadError;
87 }
88
89 /// ParseConditionalAssemblyDirectives - parse the conditional assembly
90 /// directives
91 bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
92                                                    SMLoc DirectiveLoc) {
93   if (Directive == ".if")
94     return ParseDirectiveIf(DirectiveLoc);
95   if (Directive == ".elseif")
96     return ParseDirectiveElseIf(DirectiveLoc);
97   if (Directive == ".else")
98     return ParseDirectiveElse(DirectiveLoc);
99   if (Directive == ".endif")
100     return ParseDirectiveEndIf(DirectiveLoc);
101   return true;
102 }
103
104 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
105 void AsmParser::EatToEndOfStatement() {
106   while (Lexer.isNot(AsmToken::EndOfStatement) &&
107          Lexer.isNot(AsmToken::Eof))
108     Lexer.Lex();
109   
110   // Eat EOL.
111   if (Lexer.is(AsmToken::EndOfStatement))
112     Lexer.Lex();
113 }
114
115
116 /// ParseParenExpr - Parse a paren expression and return it.
117 /// NOTE: This assumes the leading '(' has already been consumed.
118 ///
119 /// parenexpr ::= expr)
120 ///
121 bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
122   if (ParseExpression(Res)) return true;
123   if (Lexer.isNot(AsmToken::RParen))
124     return TokError("expected ')' in parentheses expression");
125   Lexer.Lex();
126   return false;
127 }
128
129 /// ParsePrimaryExpr - Parse a primary expression and return it.
130 ///  primaryexpr ::= (parenexpr
131 ///  primaryexpr ::= symbol
132 ///  primaryexpr ::= number
133 ///  primaryexpr ::= ~,+,- primaryexpr
134 bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
135   switch (Lexer.getKind()) {
136   default:
137     return TokError("unknown token in expression");
138   case AsmToken::Exclaim:
139     Lexer.Lex(); // Eat the operator.
140     if (ParsePrimaryExpr(Res))
141       return true;
142     Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
143     return false;
144   case AsmToken::String:
145   case AsmToken::Identifier: {
146     // This is a label, this should be parsed as part of an expression, to
147     // handle things like LFOO+4.
148     MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getIdentifier());
149
150     // If this is use of an undefined symbol then mark it external.
151     if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
152       Sym->setExternal(true);
153     
154     Res = new AsmSymbolRefExpr(Sym);
155     Lexer.Lex(); // Eat identifier.
156     return false;
157   }
158   case AsmToken::Integer:
159     Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
160     Lexer.Lex(); // Eat token.
161     return false;
162   case AsmToken::LParen:
163     Lexer.Lex(); // Eat the '('.
164     return ParseParenExpr(Res);
165   case AsmToken::Minus:
166     Lexer.Lex(); // Eat the operator.
167     if (ParsePrimaryExpr(Res))
168       return true;
169     Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
170     return false;
171   case AsmToken::Plus:
172     Lexer.Lex(); // Eat the operator.
173     if (ParsePrimaryExpr(Res))
174       return true;
175     Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
176     return false;
177   case AsmToken::Tilde:
178     Lexer.Lex(); // Eat the operator.
179     if (ParsePrimaryExpr(Res))
180       return true;
181     Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
182     return false;
183   }
184 }
185
186 /// ParseExpression - Parse an expression and return it.
187 /// 
188 ///  expr ::= expr +,- expr          -> lowest.
189 ///  expr ::= expr |,^,&,! expr      -> middle.
190 ///  expr ::= expr *,/,%,<<,>> expr  -> highest.
191 ///  expr ::= primaryexpr
192 ///
193 bool AsmParser::ParseExpression(AsmExpr *&Res) {
194   Res = 0;
195   return ParsePrimaryExpr(Res) ||
196          ParseBinOpRHS(1, Res);
197 }
198
199 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
200   AsmExpr *Expr;
201   
202   SMLoc StartLoc = Lexer.getLoc();
203   if (ParseExpression(Expr))
204     return true;
205
206   if (!Expr->EvaluateAsAbsolute(Ctx, Res))
207     return Error(StartLoc, "expected absolute expression");
208
209   return false;
210 }
211
212 bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
213   AsmExpr *Expr;
214   
215   SMLoc StartLoc = Lexer.getLoc();
216   if (ParseExpression(Expr))
217     return true;
218
219   if (!Expr->EvaluateAsRelocatable(Ctx, Res))
220     return Error(StartLoc, "expected relocatable expression");
221
222   return false;
223 }
224
225 bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
226   AsmExpr *Expr;
227   
228   SMLoc StartLoc = Lexer.getLoc();
229   if (ParseParenExpr(Expr))
230     return true;
231
232   if (!Expr->EvaluateAsRelocatable(Ctx, Res))
233     return Error(StartLoc, "expected relocatable expression");
234
235   return false;
236 }
237
238 static unsigned getBinOpPrecedence(AsmToken::TokenKind K, 
239                                    AsmBinaryExpr::Opcode &Kind) {
240   switch (K) {
241   default: return 0;    // not a binop.
242
243     // Lowest Precedence: &&, ||
244   case AsmToken::AmpAmp:
245     Kind = AsmBinaryExpr::LAnd;
246     return 1;
247   case AsmToken::PipePipe:
248     Kind = AsmBinaryExpr::LOr;
249     return 1;
250
251     // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
252   case AsmToken::Plus:
253     Kind = AsmBinaryExpr::Add;
254     return 2;
255   case AsmToken::Minus:
256     Kind = AsmBinaryExpr::Sub;
257     return 2;
258   case AsmToken::EqualEqual:
259     Kind = AsmBinaryExpr::EQ;
260     return 2;
261   case AsmToken::ExclaimEqual:
262   case AsmToken::LessGreater:
263     Kind = AsmBinaryExpr::NE;
264     return 2;
265   case AsmToken::Less:
266     Kind = AsmBinaryExpr::LT;
267     return 2;
268   case AsmToken::LessEqual:
269     Kind = AsmBinaryExpr::LTE;
270     return 2;
271   case AsmToken::Greater:
272     Kind = AsmBinaryExpr::GT;
273     return 2;
274   case AsmToken::GreaterEqual:
275     Kind = AsmBinaryExpr::GTE;
276     return 2;
277
278     // Intermediate Precedence: |, &, ^
279     //
280     // FIXME: gas seems to support '!' as an infix operator?
281   case AsmToken::Pipe:
282     Kind = AsmBinaryExpr::Or;
283     return 3;
284   case AsmToken::Caret:
285     Kind = AsmBinaryExpr::Xor;
286     return 3;
287   case AsmToken::Amp:
288     Kind = AsmBinaryExpr::And;
289     return 3;
290
291     // Highest Precedence: *, /, %, <<, >>
292   case AsmToken::Star:
293     Kind = AsmBinaryExpr::Mul;
294     return 4;
295   case AsmToken::Slash:
296     Kind = AsmBinaryExpr::Div;
297     return 4;
298   case AsmToken::Percent:
299     Kind = AsmBinaryExpr::Mod;
300     return 4;
301   case AsmToken::LessLess:
302     Kind = AsmBinaryExpr::Shl;
303     return 4;
304   case AsmToken::GreaterGreater:
305     Kind = AsmBinaryExpr::Shr;
306     return 4;
307   }
308 }
309
310
311 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
312 /// Res contains the LHS of the expression on input.
313 bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
314   while (1) {
315     AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
316     unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
317     
318     // If the next token is lower precedence than we are allowed to eat, return
319     // successfully with what we ate already.
320     if (TokPrec < Precedence)
321       return false;
322     
323     Lexer.Lex();
324     
325     // Eat the next primary expression.
326     AsmExpr *RHS;
327     if (ParsePrimaryExpr(RHS)) return true;
328     
329     // If BinOp binds less tightly with RHS than the operator after RHS, let
330     // the pending operator take RHS as its LHS.
331     AsmBinaryExpr::Opcode Dummy;
332     unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
333     if (TokPrec < NextTokPrec) {
334       if (ParseBinOpRHS(Precedence+1, RHS)) return true;
335     }
336
337     // Merge LHS and RHS according to operator.
338     Res = new AsmBinaryExpr(Kind, Res, RHS);
339   }
340 }
341
342   
343   
344   
345 /// ParseStatement:
346 ///   ::= EndOfStatement
347 ///   ::= Label* Directive ...Operands... EndOfStatement
348 ///   ::= Label* Identifier OperandList* EndOfStatement
349 bool AsmParser::ParseStatement() {
350   if (Lexer.is(AsmToken::EndOfStatement)) {
351     Lexer.Lex();
352     return false;
353   }
354
355   // Statements always start with an identifier.
356   AsmToken ID = Lexer.getTok();
357   SMLoc IDLoc = ID.getLoc();
358   StringRef IDVal;
359   if (ParseIdentifier(IDVal))
360     return TokError("unexpected token at start of statement");
361
362   // FIXME: Recurse on local labels?
363
364   // See what kind of statement we have.
365   switch (Lexer.getKind()) {
366   case AsmToken::Colon: {
367     // identifier ':'   -> Label.
368     Lexer.Lex();
369
370     // Diagnose attempt to use a variable as a label.
371     //
372     // FIXME: Diagnostics. Note the location of the definition as a label.
373     // FIXME: This doesn't diagnose assignment to a symbol which has been
374     // implicitly marked as external.
375     MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
376     if (Sym->getSection())
377       return Error(IDLoc, "invalid symbol redefinition");
378     if (Ctx.GetSymbolValue(Sym))
379       return Error(IDLoc, "symbol already used as assembler variable");
380     
381     // Since we saw a label, create a symbol and emit it.
382     // FIXME: If the label starts with L it is an assembler temporary label.
383     // Why does the client of this api need to know this?
384     Out.EmitLabel(Sym);
385    
386     return ParseStatement();
387   }
388
389   case AsmToken::Equal:
390     // identifier '=' ... -> assignment statement
391     Lexer.Lex();
392
393     return ParseAssignment(IDVal, false);
394
395   default: // Normal instruction or directive.
396     break;
397   }
398   
399   // Otherwise, we have a normal instruction or directive.  
400   if (IDVal[0] == '.') {
401     // FIXME: This should be driven based on a hash lookup and callback.
402     if (IDVal == ".section")
403       return ParseDirectiveDarwinSection();
404     if (IDVal == ".text")
405       // FIXME: This changes behavior based on the -static flag to the
406       // assembler.
407       return ParseDirectiveSectionSwitch("__TEXT,__text",
408                                          "regular,pure_instructions");
409     if (IDVal == ".const")
410       return ParseDirectiveSectionSwitch("__TEXT,__const");
411     if (IDVal == ".static_const")
412       return ParseDirectiveSectionSwitch("__TEXT,__static_const");
413     if (IDVal == ".cstring")
414       return ParseDirectiveSectionSwitch("__TEXT,__cstring", 
415                                          "cstring_literals");
416     if (IDVal == ".literal4")
417       return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
418     if (IDVal == ".literal8")
419       return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
420     if (IDVal == ".literal16")
421       return ParseDirectiveSectionSwitch("__TEXT,__literal16",
422                                          "16byte_literals");
423     if (IDVal == ".constructor")
424       return ParseDirectiveSectionSwitch("__TEXT,__constructor");
425     if (IDVal == ".destructor")
426       return ParseDirectiveSectionSwitch("__TEXT,__destructor");
427     if (IDVal == ".fvmlib_init0")
428       return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
429     if (IDVal == ".fvmlib_init1")
430       return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
431     if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
432       return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
433                                     "self_modifying_code+pure_instructions,5");
434     // FIXME: .picsymbol_stub on PPC.
435     if (IDVal == ".data")
436       return ParseDirectiveSectionSwitch("__DATA,__data");
437     if (IDVal == ".static_data")
438       return ParseDirectiveSectionSwitch("__DATA,__static_data");
439     if (IDVal == ".non_lazy_symbol_pointer")
440       return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
441                                          "non_lazy_symbol_pointers");
442     if (IDVal == ".lazy_symbol_pointer")
443       return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
444                                          "lazy_symbol_pointers");
445     if (IDVal == ".dyld")
446       return ParseDirectiveSectionSwitch("__DATA,__dyld");
447     if (IDVal == ".mod_init_func")
448       return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
449                                          "mod_init_funcs");
450     if (IDVal == ".mod_term_func")
451       return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
452                                          "mod_term_funcs");
453     if (IDVal == ".const_data")
454       return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
455     
456     
457     // FIXME: Verify attributes on sections.
458     if (IDVal == ".objc_class")
459       return ParseDirectiveSectionSwitch("__OBJC,__class");
460     if (IDVal == ".objc_meta_class")
461       return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
462     if (IDVal == ".objc_cat_cls_meth")
463       return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
464     if (IDVal == ".objc_cat_inst_meth")
465       return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
466     if (IDVal == ".objc_protocol")
467       return ParseDirectiveSectionSwitch("__OBJC,__protocol");
468     if (IDVal == ".objc_string_object")
469       return ParseDirectiveSectionSwitch("__OBJC,__string_object");
470     if (IDVal == ".objc_cls_meth")
471       return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
472     if (IDVal == ".objc_inst_meth")
473       return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
474     if (IDVal == ".objc_cls_refs")
475       return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
476     if (IDVal == ".objc_message_refs")
477       return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
478     if (IDVal == ".objc_symbols")
479       return ParseDirectiveSectionSwitch("__OBJC,__symbols");
480     if (IDVal == ".objc_category")
481       return ParseDirectiveSectionSwitch("__OBJC,__category");
482     if (IDVal == ".objc_class_vars")
483       return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
484     if (IDVal == ".objc_instance_vars")
485       return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
486     if (IDVal == ".objc_module_info")
487       return ParseDirectiveSectionSwitch("__OBJC,__module_info");
488     if (IDVal == ".objc_class_names")
489       return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
490     if (IDVal == ".objc_meth_var_types")
491       return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
492     if (IDVal == ".objc_meth_var_names")
493       return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
494     if (IDVal == ".objc_selector_strs")
495       return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
496     
497     // Assembler features
498     if (IDVal == ".set")
499       return ParseDirectiveSet();
500
501     // Data directives
502
503     if (IDVal == ".ascii")
504       return ParseDirectiveAscii(false);
505     if (IDVal == ".asciz")
506       return ParseDirectiveAscii(true);
507
508     // FIXME: Target hooks for size? Also for "word", "hword".
509     if (IDVal == ".byte")
510       return ParseDirectiveValue(1);
511     if (IDVal == ".short")
512       return ParseDirectiveValue(2);
513     if (IDVal == ".long")
514       return ParseDirectiveValue(4);
515     if (IDVal == ".quad")
516       return ParseDirectiveValue(8);
517
518     // FIXME: Target hooks for IsPow2.
519     if (IDVal == ".align")
520       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
521     if (IDVal == ".align32")
522       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
523     if (IDVal == ".balign")
524       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
525     if (IDVal == ".balignw")
526       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
527     if (IDVal == ".balignl")
528       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
529     if (IDVal == ".p2align")
530       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
531     if (IDVal == ".p2alignw")
532       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
533     if (IDVal == ".p2alignl")
534       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
535
536     if (IDVal == ".org")
537       return ParseDirectiveOrg();
538
539     if (IDVal == ".fill")
540       return ParseDirectiveFill();
541     if (IDVal == ".space")
542       return ParseDirectiveSpace();
543
544     // Symbol attribute directives
545     if (IDVal == ".globl" || IDVal == ".global")
546       return ParseDirectiveSymbolAttribute(MCStreamer::Global);
547     if (IDVal == ".hidden")
548       return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
549     if (IDVal == ".indirect_symbol")
550       return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
551     if (IDVal == ".internal")
552       return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
553     if (IDVal == ".lazy_reference")
554       return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
555     if (IDVal == ".no_dead_strip")
556       return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
557     if (IDVal == ".private_extern")
558       return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
559     if (IDVal == ".protected")
560       return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
561     if (IDVal == ".reference")
562       return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
563     if (IDVal == ".weak")
564       return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
565     if (IDVal == ".weak_definition")
566       return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
567     if (IDVal == ".weak_reference")
568       return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
569
570     if (IDVal == ".comm")
571       return ParseDirectiveComm(/*IsLocal=*/false);
572     if (IDVal == ".lcomm")
573       return ParseDirectiveComm(/*IsLocal=*/true);
574     if (IDVal == ".zerofill")
575       return ParseDirectiveDarwinZerofill();
576     if (IDVal == ".desc")
577       return ParseDirectiveDarwinSymbolDesc();
578     if (IDVal == ".lsym")
579       return ParseDirectiveDarwinLsym();
580
581     if (IDVal == ".subsections_via_symbols")
582       return ParseDirectiveDarwinSubsectionsViaSymbols();
583     if (IDVal == ".abort")
584       return ParseDirectiveAbort();
585     if (IDVal == ".include")
586       return ParseDirectiveInclude();
587     if (IDVal == ".dump")
588       return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
589     if (IDVal == ".load")
590       return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
591
592     Warning(IDLoc, "ignoring directive for now");
593     EatToEndOfStatement();
594     return false;
595   }
596
597   MCInst Inst;
598   if (getTargetParser().ParseInstruction(IDVal, Inst))
599     return true;
600   
601   if (Lexer.isNot(AsmToken::EndOfStatement))
602     return TokError("unexpected token in argument list");
603
604   // Eat the end of statement marker.
605   Lexer.Lex();
606   
607   // Instruction is good, process it.
608   Out.EmitInstruction(Inst);
609   
610   // Skip to end of line for now.
611   return false;
612 }
613
614 bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
615   // FIXME: Use better location, we should use proper tokens.
616   SMLoc EqualLoc = Lexer.getLoc();
617
618   MCValue Value;
619   if (ParseRelocatableExpression(Value))
620     return true;
621   
622   if (Lexer.isNot(AsmToken::EndOfStatement))
623     return TokError("unexpected token in assignment");
624
625   // Eat the end of statement marker.
626   Lexer.Lex();
627
628   // Diagnose assignment to a label.
629   //
630   // FIXME: Diagnostics. Note the location of the definition as a label.
631   // FIXME: This doesn't diagnose assignment to a symbol which has been
632   // implicitly marked as external.
633   // FIXME: Handle '.'.
634   // FIXME: Diagnose assignment to protected identifier (e.g., register name).
635   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
636   if (Sym->getSection())
637     return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
638   if (Sym->isExternal())
639     return Error(EqualLoc, "invalid assignment to external symbol");
640
641   // Do the assignment.
642   Out.EmitAssignment(Sym, Value, IsDotSet);
643
644   return false;
645 }
646
647 /// ParseIdentifier:
648 ///   ::= identifier
649 ///   ::= string
650 bool AsmParser::ParseIdentifier(StringRef &Res) {
651   if (Lexer.isNot(AsmToken::Identifier) &&
652       Lexer.isNot(AsmToken::String))
653     return true;
654
655   Res = Lexer.getTok().getIdentifier();
656
657   Lexer.Lex(); // Consume the identifier token.
658
659   return false;
660 }
661
662 /// ParseDirectiveSet:
663 ///   ::= .set identifier ',' expression
664 bool AsmParser::ParseDirectiveSet() {
665   StringRef Name;
666
667   if (ParseIdentifier(Name))
668     return TokError("expected identifier after '.set' directive");
669   
670   if (Lexer.isNot(AsmToken::Comma))
671     return TokError("unexpected token in '.set'");
672   Lexer.Lex();
673
674   return ParseAssignment(Name, true);
675 }
676
677 /// ParseDirectiveSection:
678 ///   ::= .section identifier (',' identifier)*
679 /// FIXME: This should actually parse out the segment, section, attributes and
680 /// sizeof_stub fields.
681 bool AsmParser::ParseDirectiveDarwinSection() {
682   StringRef SectionName;
683
684   if (ParseIdentifier(SectionName))
685     return TokError("expected identifier after '.section' directive");
686   
687   std::string Section = SectionName;
688
689   // FIXME: This doesn't work, we lose quoting on things
690
691   // Accept a comma separated list of modifiers.
692   while (Lexer.is(AsmToken::Comma)) {
693     Lexer.Lex(); // Consume the comma.
694
695     StringRef ModifierName;    
696     if (ParseIdentifier(ModifierName))
697       return TokError("expected identifier in '.section' directive");
698     Section += ',';
699     Section += ModifierName;
700   }
701   
702   if (Lexer.isNot(AsmToken::EndOfStatement))
703     return TokError("unexpected token in '.section' directive");
704   Lexer.Lex();
705
706   // FIXME: Arch specific.
707   MCSection *S = Ctx.GetSection(Section);
708   if (S == 0)
709     S = MCSection::Create(Section, false, SectionKind(), Ctx);
710   
711   Out.SwitchSection(S);
712   return false;
713 }
714
715 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
716                                             const char *Directives) {
717   if (Lexer.isNot(AsmToken::EndOfStatement))
718     return TokError("unexpected token in section switching directive");
719   Lexer.Lex();
720   
721   std::string SectionStr = Section;
722   if (Directives && Directives[0]) {
723     SectionStr += ","; 
724     SectionStr += Directives;
725   }
726   
727   // FIXME: Arch specific.
728   MCSection *S = Ctx.GetSection(Section);
729   if (S == 0)
730     S = MCSection::Create(Section, false, SectionKind(), Ctx);
731   
732   Out.SwitchSection(S);
733   return false;
734 }
735
736 /// ParseDirectiveAscii:
737 ///   ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
738 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
739   if (Lexer.isNot(AsmToken::EndOfStatement)) {
740     for (;;) {
741       if (Lexer.isNot(AsmToken::String))
742         return TokError("expected string in '.ascii' or '.asciz' directive");
743       
744       // FIXME: This shouldn't use a const char* + strlen, the string could have
745       // embedded nulls.
746       // FIXME: Should have accessor for getting string contents.
747       StringRef Str = Lexer.getTok().getString();
748       Out.EmitBytes(Str.substr(1, Str.size() - 2));
749       if (ZeroTerminated)
750         Out.EmitBytes(StringRef("\0", 1));
751       
752       Lexer.Lex();
753       
754       if (Lexer.is(AsmToken::EndOfStatement))
755         break;
756
757       if (Lexer.isNot(AsmToken::Comma))
758         return TokError("unexpected token in '.ascii' or '.asciz' directive");
759       Lexer.Lex();
760     }
761   }
762
763   Lexer.Lex();
764   return false;
765 }
766
767 /// ParseDirectiveValue
768 ///  ::= (.byte | .short | ... ) [ expression (, expression)* ]
769 bool AsmParser::ParseDirectiveValue(unsigned Size) {
770   if (Lexer.isNot(AsmToken::EndOfStatement)) {
771     for (;;) {
772       MCValue Expr;
773       if (ParseRelocatableExpression(Expr))
774         return true;
775
776       Out.EmitValue(Expr, Size);
777
778       if (Lexer.is(AsmToken::EndOfStatement))
779         break;
780       
781       // FIXME: Improve diagnostic.
782       if (Lexer.isNot(AsmToken::Comma))
783         return TokError("unexpected token in directive");
784       Lexer.Lex();
785     }
786   }
787
788   Lexer.Lex();
789   return false;
790 }
791
792 /// ParseDirectiveSpace
793 ///  ::= .space expression [ , expression ]
794 bool AsmParser::ParseDirectiveSpace() {
795   int64_t NumBytes;
796   if (ParseAbsoluteExpression(NumBytes))
797     return true;
798
799   int64_t FillExpr = 0;
800   bool HasFillExpr = false;
801   if (Lexer.isNot(AsmToken::EndOfStatement)) {
802     if (Lexer.isNot(AsmToken::Comma))
803       return TokError("unexpected token in '.space' directive");
804     Lexer.Lex();
805     
806     if (ParseAbsoluteExpression(FillExpr))
807       return true;
808
809     HasFillExpr = true;
810
811     if (Lexer.isNot(AsmToken::EndOfStatement))
812       return TokError("unexpected token in '.space' directive");
813   }
814
815   Lexer.Lex();
816
817   if (NumBytes <= 0)
818     return TokError("invalid number of bytes in '.space' directive");
819
820   // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
821   for (uint64_t i = 0, e = NumBytes; i != e; ++i)
822     Out.EmitValue(MCValue::get(FillExpr), 1);
823
824   return false;
825 }
826
827 /// ParseDirectiveFill
828 ///  ::= .fill expression , expression , expression
829 bool AsmParser::ParseDirectiveFill() {
830   int64_t NumValues;
831   if (ParseAbsoluteExpression(NumValues))
832     return true;
833
834   if (Lexer.isNot(AsmToken::Comma))
835     return TokError("unexpected token in '.fill' directive");
836   Lexer.Lex();
837   
838   int64_t FillSize;
839   if (ParseAbsoluteExpression(FillSize))
840     return true;
841
842   if (Lexer.isNot(AsmToken::Comma))
843     return TokError("unexpected token in '.fill' directive");
844   Lexer.Lex();
845   
846   int64_t FillExpr;
847   if (ParseAbsoluteExpression(FillExpr))
848     return true;
849
850   if (Lexer.isNot(AsmToken::EndOfStatement))
851     return TokError("unexpected token in '.fill' directive");
852   
853   Lexer.Lex();
854
855   if (FillSize != 1 && FillSize != 2 && FillSize != 4)
856     return TokError("invalid '.fill' size, expected 1, 2, or 4");
857
858   for (uint64_t i = 0, e = NumValues; i != e; ++i)
859     Out.EmitValue(MCValue::get(FillExpr), FillSize);
860
861   return false;
862 }
863
864 /// ParseDirectiveOrg
865 ///  ::= .org expression [ , expression ]
866 bool AsmParser::ParseDirectiveOrg() {
867   MCValue Offset;
868   if (ParseRelocatableExpression(Offset))
869     return true;
870
871   // Parse optional fill expression.
872   int64_t FillExpr = 0;
873   if (Lexer.isNot(AsmToken::EndOfStatement)) {
874     if (Lexer.isNot(AsmToken::Comma))
875       return TokError("unexpected token in '.org' directive");
876     Lexer.Lex();
877     
878     if (ParseAbsoluteExpression(FillExpr))
879       return true;
880
881     if (Lexer.isNot(AsmToken::EndOfStatement))
882       return TokError("unexpected token in '.org' directive");
883   }
884
885   Lexer.Lex();
886
887   // FIXME: Only limited forms of relocatable expressions are accepted here, it
888   // has to be relative to the current section.
889   Out.EmitValueToOffset(Offset, FillExpr);
890
891   return false;
892 }
893
894 /// ParseDirectiveAlign
895 ///  ::= {.align, ...} expression [ , expression [ , expression ]]
896 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
897   int64_t Alignment;
898   if (ParseAbsoluteExpression(Alignment))
899     return true;
900
901   SMLoc MaxBytesLoc;
902   bool HasFillExpr = false;
903   int64_t FillExpr = 0;
904   int64_t MaxBytesToFill = 0;
905   if (Lexer.isNot(AsmToken::EndOfStatement)) {
906     if (Lexer.isNot(AsmToken::Comma))
907       return TokError("unexpected token in directive");
908     Lexer.Lex();
909
910     // The fill expression can be omitted while specifying a maximum number of
911     // alignment bytes, e.g:
912     //  .align 3,,4
913     if (Lexer.isNot(AsmToken::Comma)) {
914       HasFillExpr = true;
915       if (ParseAbsoluteExpression(FillExpr))
916         return true;
917     }
918
919     if (Lexer.isNot(AsmToken::EndOfStatement)) {
920       if (Lexer.isNot(AsmToken::Comma))
921         return TokError("unexpected token in directive");
922       Lexer.Lex();
923
924       MaxBytesLoc = Lexer.getLoc();
925       if (ParseAbsoluteExpression(MaxBytesToFill))
926         return true;
927       
928       if (Lexer.isNot(AsmToken::EndOfStatement))
929         return TokError("unexpected token in directive");
930     }
931   }
932
933   Lexer.Lex();
934
935   if (!HasFillExpr) {
936     // FIXME: Sometimes fill with nop.
937     FillExpr = 0;
938   }
939
940   // Compute alignment in bytes.
941   if (IsPow2) {
942     // FIXME: Diagnose overflow.
943     Alignment = 1LL << Alignment;
944   }
945
946   // Diagnose non-sensical max bytes to fill.
947   if (MaxBytesLoc.isValid()) {
948     if (MaxBytesToFill < 1) {
949       Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
950               "many bytes, ignoring");
951       return false;
952     }
953
954     if (MaxBytesToFill >= Alignment) {
955       Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
956               "has no effect");
957       MaxBytesToFill = 0;
958     }
959   }
960
961   // FIXME: Target specific behavior about how the "extra" bytes are filled.
962   Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
963
964   return false;
965 }
966
967 /// ParseDirectiveSymbolAttribute
968 ///  ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
969 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
970   if (Lexer.isNot(AsmToken::EndOfStatement)) {
971     for (;;) {
972       StringRef Name;
973
974       if (ParseIdentifier(Name))
975         return TokError("expected identifier in directive");
976       
977       MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
978
979       // If this is use of an undefined symbol then mark it external.
980       if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
981         Sym->setExternal(true);
982
983       Out.EmitSymbolAttribute(Sym, Attr);
984
985       if (Lexer.is(AsmToken::EndOfStatement))
986         break;
987
988       if (Lexer.isNot(AsmToken::Comma))
989         return TokError("unexpected token in directive");
990       Lexer.Lex();
991     }
992   }
993
994   Lexer.Lex();
995   return false;  
996 }
997
998 /// ParseDirectiveDarwinSymbolDesc
999 ///  ::= .desc identifier , expression
1000 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
1001   StringRef Name;
1002   if (ParseIdentifier(Name))
1003     return TokError("expected identifier in directive");
1004   
1005   // Handle the identifier as the key symbol.
1006   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
1007
1008   if (Lexer.isNot(AsmToken::Comma))
1009     return TokError("unexpected token in '.desc' directive");
1010   Lexer.Lex();
1011
1012   SMLoc DescLoc = Lexer.getLoc();
1013   int64_t DescValue;
1014   if (ParseAbsoluteExpression(DescValue))
1015     return true;
1016
1017   if (Lexer.isNot(AsmToken::EndOfStatement))
1018     return TokError("unexpected token in '.desc' directive");
1019   
1020   Lexer.Lex();
1021
1022   // Set the n_desc field of this Symbol to this DescValue
1023   Out.EmitSymbolDesc(Sym, DescValue);
1024
1025   return false;
1026 }
1027
1028 /// ParseDirectiveComm
1029 ///  ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1030 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1031   SMLoc IDLoc = Lexer.getLoc();
1032   StringRef Name;
1033   if (ParseIdentifier(Name))
1034     return TokError("expected identifier in directive");
1035   
1036   // Handle the identifier as the key symbol.
1037   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
1038
1039   if (Lexer.isNot(AsmToken::Comma))
1040     return TokError("unexpected token in directive");
1041   Lexer.Lex();
1042
1043   int64_t Size;
1044   SMLoc SizeLoc = Lexer.getLoc();
1045   if (ParseAbsoluteExpression(Size))
1046     return true;
1047
1048   int64_t Pow2Alignment = 0;
1049   SMLoc Pow2AlignmentLoc;
1050   if (Lexer.is(AsmToken::Comma)) {
1051     Lexer.Lex();
1052     Pow2AlignmentLoc = Lexer.getLoc();
1053     if (ParseAbsoluteExpression(Pow2Alignment))
1054       return true;
1055   }
1056   
1057   if (Lexer.isNot(AsmToken::EndOfStatement))
1058     return TokError("unexpected token in '.comm' or '.lcomm' directive");
1059   
1060   Lexer.Lex();
1061
1062   // NOTE: a size of zero for a .comm should create a undefined symbol
1063   // but a size of .lcomm creates a bss symbol of size zero.
1064   if (Size < 0)
1065     return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1066                  "be less than zero");
1067
1068   // NOTE: The alignment in the directive is a power of 2 value, the assember
1069   // may internally end up wanting an alignment in bytes.
1070   // FIXME: Diagnose overflow.
1071   if (Pow2Alignment < 0)
1072     return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1073                  "alignment, can't be less than zero");
1074
1075   // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1076   if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1077     return Error(IDLoc, "invalid symbol redefinition");
1078
1079   // Create the Symbol as a common or local common with Size and Pow2Alignment
1080   Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
1081
1082   return false;
1083 }
1084
1085 /// ParseDirectiveDarwinZerofill
1086 ///  ::= .zerofill segname , sectname [, identifier , size_expression [
1087 ///      , align_expression ]]
1088 bool AsmParser::ParseDirectiveDarwinZerofill() {
1089   // FIXME: Handle quoted names here.
1090
1091   if (Lexer.isNot(AsmToken::Identifier))
1092     return TokError("expected segment name after '.zerofill' directive");
1093   std::string Section = Lexer.getTok().getString();
1094   Lexer.Lex();
1095
1096   if (Lexer.isNot(AsmToken::Comma))
1097     return TokError("unexpected token in directive");
1098   Section += ',';
1099   Lexer.Lex();
1100  
1101   if (Lexer.isNot(AsmToken::Identifier))
1102     return TokError("expected section name after comma in '.zerofill' "
1103                     "directive");
1104   Section += Lexer.getTok().getString().str();
1105   Lexer.Lex();
1106
1107   // FIXME: we will need to tell GetSection() that this is to be created with or
1108   // must have the Mach-O section type of S_ZEROFILL.  Something like the code
1109   // below could be done but for now it is not as EmitZerofill() does not know
1110   // how to deal with a section type in the section name like
1111   // ParseDirectiveDarwinSection() allows.
1112   // Section += ',';
1113   // Section += "zerofill";
1114
1115   // If this is the end of the line all that was wanted was to create the
1116   // the section but with no symbol.
1117   if (Lexer.is(AsmToken::EndOfStatement)) {
1118     // FIXME: Arch specific.
1119     MCSection *S = Ctx.GetSection(Section);
1120     if (S == 0)
1121       S = MCSection::Create(Section, false, SectionKind(), Ctx);
1122     
1123     // Create the zerofill section but no symbol
1124     Out.EmitZerofill(S);
1125     return false;
1126   }
1127
1128   if (Lexer.isNot(AsmToken::Comma))
1129     return TokError("unexpected token in directive");
1130   Lexer.Lex();
1131
1132   if (Lexer.isNot(AsmToken::Identifier))
1133     return TokError("expected identifier in directive");
1134   
1135   // handle the identifier as the key symbol.
1136   SMLoc IDLoc = Lexer.getLoc();
1137   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
1138   Lexer.Lex();
1139
1140   if (Lexer.isNot(AsmToken::Comma))
1141     return TokError("unexpected token in directive");
1142   Lexer.Lex();
1143
1144   int64_t Size;
1145   SMLoc SizeLoc = Lexer.getLoc();
1146   if (ParseAbsoluteExpression(Size))
1147     return true;
1148
1149   int64_t Pow2Alignment = 0;
1150   SMLoc Pow2AlignmentLoc;
1151   if (Lexer.is(AsmToken::Comma)) {
1152     Lexer.Lex();
1153     Pow2AlignmentLoc = Lexer.getLoc();
1154     if (ParseAbsoluteExpression(Pow2Alignment))
1155       return true;
1156   }
1157   
1158   if (Lexer.isNot(AsmToken::EndOfStatement))
1159     return TokError("unexpected token in '.zerofill' directive");
1160   
1161   Lexer.Lex();
1162
1163   if (Size < 0)
1164     return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1165                  "than zero");
1166
1167   // NOTE: The alignment in the directive is a power of 2 value, the assember
1168   // may internally end up wanting an alignment in bytes.
1169   // FIXME: Diagnose overflow.
1170   if (Pow2Alignment < 0)
1171     return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1172                  "can't be less than zero");
1173
1174   // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1175   if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1176     return Error(IDLoc, "invalid symbol redefinition");
1177
1178   // FIXME: Arch specific.
1179   MCSection *S = Ctx.GetSection(Section);
1180   if (S == 0)
1181     S = MCSection::Create(Section, false, SectionKind(), Ctx);
1182   
1183   // Create the zerofill Symbol with Size and Pow2Alignment
1184   Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
1185
1186   return false;
1187 }
1188
1189 /// ParseDirectiveDarwinSubsectionsViaSymbols
1190 ///  ::= .subsections_via_symbols
1191 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1192   if (Lexer.isNot(AsmToken::EndOfStatement))
1193     return TokError("unexpected token in '.subsections_via_symbols' directive");
1194   
1195   Lexer.Lex();
1196
1197   Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1198
1199   return false;
1200 }
1201
1202 /// ParseDirectiveAbort
1203 ///  ::= .abort [ "abort_string" ]
1204 bool AsmParser::ParseDirectiveAbort() {
1205   // FIXME: Use loc from directive.
1206   SMLoc Loc = Lexer.getLoc();
1207
1208   StringRef Str = "";
1209   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1210     if (Lexer.isNot(AsmToken::String))
1211       return TokError("expected string in '.abort' directive");
1212     
1213     Str = Lexer.getTok().getString();
1214
1215     Lexer.Lex();
1216   }
1217
1218   if (Lexer.isNot(AsmToken::EndOfStatement))
1219     return TokError("unexpected token in '.abort' directive");
1220   
1221   Lexer.Lex();
1222
1223   // FIXME: Handle here.
1224   if (Str.empty())
1225     Error(Loc, ".abort detected. Assembly stopping.");
1226   else
1227     Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1228
1229   return false;
1230 }
1231
1232 /// ParseDirectiveLsym
1233 ///  ::= .lsym identifier , expression
1234 bool AsmParser::ParseDirectiveDarwinLsym() {
1235   StringRef Name;
1236   if (ParseIdentifier(Name))
1237     return TokError("expected identifier in directive");
1238   
1239   // Handle the identifier as the key symbol.
1240   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
1241
1242   if (Lexer.isNot(AsmToken::Comma))
1243     return TokError("unexpected token in '.lsym' directive");
1244   Lexer.Lex();
1245
1246   MCValue Expr;
1247   if (ParseRelocatableExpression(Expr))
1248     return true;
1249
1250   if (Lexer.isNot(AsmToken::EndOfStatement))
1251     return TokError("unexpected token in '.lsym' directive");
1252   
1253   Lexer.Lex();
1254
1255   // Create the Sym with the value of the Expr
1256   Out.EmitLocalSymbol(Sym, Expr);
1257
1258   return false;
1259 }
1260
1261 /// ParseDirectiveInclude
1262 ///  ::= .include "filename"
1263 bool AsmParser::ParseDirectiveInclude() {
1264   if (Lexer.isNot(AsmToken::String))
1265     return TokError("expected string in '.include' directive");
1266   
1267   std::string Filename = Lexer.getTok().getString();
1268   SMLoc IncludeLoc = Lexer.getLoc();
1269   Lexer.Lex();
1270
1271   if (Lexer.isNot(AsmToken::EndOfStatement))
1272     return TokError("unexpected token in '.include' directive");
1273   
1274   // Strip the quotes.
1275   Filename = Filename.substr(1, Filename.size()-2);
1276   
1277   // Attempt to switch the lexer to the included file before consuming the end
1278   // of statement to avoid losing it when we switch.
1279   if (Lexer.EnterIncludeFile(Filename)) {
1280     Lexer.PrintMessage(IncludeLoc,
1281                        "Could not find include file '" + Filename + "'",
1282                        "error");
1283     return true;
1284   }
1285
1286   return false;
1287 }
1288
1289 /// ParseDirectiveDarwinDumpOrLoad
1290 ///  ::= ( .dump | .load ) "filename"
1291 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1292   if (Lexer.isNot(AsmToken::String))
1293     return TokError("expected string in '.dump' or '.load' directive");
1294   
1295   Lexer.Lex();
1296
1297   if (Lexer.isNot(AsmToken::EndOfStatement))
1298     return TokError("unexpected token in '.dump' or '.load' directive");
1299   
1300   Lexer.Lex();
1301
1302   // FIXME: If/when .dump and .load are implemented they will be done in the
1303   // the assembly parser and not have any need for an MCStreamer API.
1304   if (IsDump)
1305     Warning(IDLoc, "ignoring directive .dump for now");
1306   else
1307     Warning(IDLoc, "ignoring directive .load for now");
1308
1309   return false;
1310 }
1311
1312 /// ParseDirectiveIf
1313 /// ::= .if expression
1314 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1315   // Consume the identifier that was the .if directive
1316   Lexer.Lex();
1317
1318   TheCondStack.push_back(TheCondState);
1319   TheCondState.TheCond = AsmCond::IfCond;
1320   if(TheCondState.Ignore) {
1321     EatToEndOfStatement();
1322   }
1323   else {
1324     int64_t ExprValue;
1325     if (ParseAbsoluteExpression(ExprValue))
1326       return true;
1327
1328     if (Lexer.isNot(AsmToken::EndOfStatement))
1329       return TokError("unexpected token in '.if' directive");
1330     
1331     Lexer.Lex();
1332
1333     TheCondState.CondMet = ExprValue;
1334     TheCondState.Ignore = !TheCondState.CondMet;
1335   }
1336
1337   return false;
1338 }
1339
1340 /// ParseDirectiveElseIf
1341 /// ::= .elseif expression
1342 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1343   if (TheCondState.TheCond != AsmCond::IfCond &&
1344       TheCondState.TheCond != AsmCond::ElseIfCond)
1345       Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1346                           " an .elseif");
1347   TheCondState.TheCond = AsmCond::ElseIfCond;
1348
1349   // Consume the identifier that was the .elseif directive
1350   Lexer.Lex();
1351
1352   bool LastIgnoreState = false;
1353   if (!TheCondStack.empty())
1354       LastIgnoreState = TheCondStack.back().Ignore;
1355   if (LastIgnoreState || TheCondState.CondMet) {
1356     TheCondState.Ignore = true;
1357     EatToEndOfStatement();
1358   }
1359   else {
1360     int64_t ExprValue;
1361     if (ParseAbsoluteExpression(ExprValue))
1362       return true;
1363
1364     if (Lexer.isNot(AsmToken::EndOfStatement))
1365       return TokError("unexpected token in '.elseif' directive");
1366     
1367     Lexer.Lex();
1368     TheCondState.CondMet = ExprValue;
1369     TheCondState.Ignore = !TheCondState.CondMet;
1370   }
1371
1372   return false;
1373 }
1374
1375 /// ParseDirectiveElse
1376 /// ::= .else
1377 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1378   // Consume the identifier that was the .else directive
1379   Lexer.Lex();
1380
1381   if (Lexer.isNot(AsmToken::EndOfStatement))
1382     return TokError("unexpected token in '.else' directive");
1383   
1384   Lexer.Lex();
1385
1386   if (TheCondState.TheCond != AsmCond::IfCond &&
1387       TheCondState.TheCond != AsmCond::ElseIfCond)
1388       Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1389                           ".elseif");
1390   TheCondState.TheCond = AsmCond::ElseCond;
1391   bool LastIgnoreState = false;
1392   if (!TheCondStack.empty())
1393     LastIgnoreState = TheCondStack.back().Ignore;
1394   if (LastIgnoreState || TheCondState.CondMet)
1395     TheCondState.Ignore = true;
1396   else
1397     TheCondState.Ignore = false;
1398
1399   return false;
1400 }
1401
1402 /// ParseDirectiveEndIf
1403 /// ::= .endif
1404 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1405   // Consume the identifier that was the .endif directive
1406   Lexer.Lex();
1407
1408   if (Lexer.isNot(AsmToken::EndOfStatement))
1409     return TokError("unexpected token in '.endif' directive");
1410   
1411   Lexer.Lex();
1412
1413   if ((TheCondState.TheCond == AsmCond::NoCond) ||
1414       TheCondStack.empty())
1415     Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1416                         ".else");
1417   if (!TheCondStack.empty()) {
1418     TheCondState = TheCondStack.back();
1419     TheCondStack.pop_back();
1420   }
1421
1422   return false;
1423 }