llvm-mc: Accept .word as a synonym for .short
[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/MCSectionMachO.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                                      MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
409     if (IDVal == ".const")
410       return ParseDirectiveSectionSwitch("__TEXT", "__const", 0);
411     if (IDVal == ".static_const")
412       return ParseDirectiveSectionSwitch("__TEXT", "__static_const", 0);
413     if (IDVal == ".cstring")
414       return ParseDirectiveSectionSwitch("__TEXT","__cstring", 
415                                          MCSectionMachO::S_CSTRING_LITERALS);
416     if (IDVal == ".literal4")
417       return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
418                                          MCSectionMachO::S_4BYTE_LITERALS);
419     if (IDVal == ".literal8")
420       return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
421                                          MCSectionMachO::S_8BYTE_LITERALS);
422     if (IDVal == ".literal16")
423       return ParseDirectiveSectionSwitch("__TEXT","__literal16",
424                                          MCSectionMachO::S_16BYTE_LITERALS);
425     if (IDVal == ".constructor")
426       return ParseDirectiveSectionSwitch("__TEXT","__constructor", 0);
427     if (IDVal == ".destructor")
428       return ParseDirectiveSectionSwitch("__TEXT","__destructor", 0);
429     if (IDVal == ".fvmlib_init0")
430       return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0", 0);
431     if (IDVal == ".fvmlib_init1")
432       return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1", 0);
433     if (IDVal == ".symbol_stub")
434       return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
435                                          MCSectionMachO::S_SYMBOL_STUBS |
436                                     MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE |
437                                        MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
438                                           // FIXME: Different on PPC and ARM.
439                                          16);
440     // FIXME: .picsymbol_stub on PPC.
441     if (IDVal == ".data")
442       return ParseDirectiveSectionSwitch("__DATA", "__data", 0);
443     if (IDVal == ".static_data")
444       return ParseDirectiveSectionSwitch("__DATA", "__static_data", 0);
445     if (IDVal == ".non_lazy_symbol_pointer")
446       return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_pointer",
447                                     MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS);
448     if (IDVal == ".lazy_symbol_pointer")
449       return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_pointer",
450                                          MCSectionMachO::S_LAZY_SYMBOL_POINTERS);
451     if (IDVal == ".dyld")
452       return ParseDirectiveSectionSwitch("__DATA", "__dyld", 0);
453     if (IDVal == ".mod_init_func")
454       return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
455                                       MCSectionMachO::S_MOD_INIT_FUNC_POINTERS);
456     if (IDVal == ".mod_term_func")
457       return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
458                                       MCSectionMachO::S_MOD_TERM_FUNC_POINTERS);
459     if (IDVal == ".const_data")
460       return ParseDirectiveSectionSwitch("__DATA", "__const", 0);
461     
462     
463     // FIXME: Verify attributes on sections.
464     if (IDVal == ".objc_class")
465       return ParseDirectiveSectionSwitch("__OBJC", "__class", 0);
466     if (IDVal == ".objc_meta_class")
467       return ParseDirectiveSectionSwitch("__OBJC", "__meta_class", 0);
468     if (IDVal == ".objc_cat_cls_meth")
469       return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth", 0);
470     if (IDVal == ".objc_cat_inst_meth")
471       return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth", 0);
472     if (IDVal == ".objc_protocol")
473       return ParseDirectiveSectionSwitch("__OBJC", "__protocol", 0);
474     if (IDVal == ".objc_string_object")
475       return ParseDirectiveSectionSwitch("__OBJC", "__string_object", 0);
476     if (IDVal == ".objc_cls_meth")
477       return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth", 0);
478     if (IDVal == ".objc_inst_meth")
479       return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth", 0);
480     if (IDVal == ".objc_cls_refs")
481       return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs", 0);
482     if (IDVal == ".objc_message_refs")
483       return ParseDirectiveSectionSwitch("__OBJC", "__message_refs", 0);
484     if (IDVal == ".objc_symbols")
485       return ParseDirectiveSectionSwitch("__OBJC", "__symbols", 0);
486     if (IDVal == ".objc_category")
487       return ParseDirectiveSectionSwitch("__OBJC", "__category", 0);
488     if (IDVal == ".objc_class_vars")
489       return ParseDirectiveSectionSwitch("__OBJC", "__class_vars", 0);
490     if (IDVal == ".objc_instance_vars")
491       return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars", 0);
492     if (IDVal == ".objc_module_info")
493       return ParseDirectiveSectionSwitch("__OBJC", "__module_info", 0);
494     if (IDVal == ".objc_class_names")
495       return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
496                                          MCSectionMachO::S_CSTRING_LITERALS);
497     if (IDVal == ".objc_meth_var_types")
498       return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
499                                          MCSectionMachO::S_CSTRING_LITERALS);
500     if (IDVal == ".objc_meth_var_names")
501       return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
502                                          MCSectionMachO::S_CSTRING_LITERALS);
503     if (IDVal == ".objc_selector_strs")
504       return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs", 0);
505     
506     // Assembler features
507     if (IDVal == ".set")
508       return ParseDirectiveSet();
509
510     // Data directives
511
512     if (IDVal == ".ascii")
513       return ParseDirectiveAscii(false);
514     if (IDVal == ".asciz")
515       return ParseDirectiveAscii(true);
516
517     // FIXME: Target hooks for size? Also for "word", "hword".
518     if (IDVal == ".byte")
519       return ParseDirectiveValue(1);
520     if (IDVal == ".short" || IDVal == ".word")
521       return ParseDirectiveValue(2);
522     if (IDVal == ".long")
523       return ParseDirectiveValue(4);
524     if (IDVal == ".quad")
525       return ParseDirectiveValue(8);
526
527     // FIXME: Target hooks for IsPow2.
528     if (IDVal == ".align")
529       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
530     if (IDVal == ".align32")
531       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
532     if (IDVal == ".balign")
533       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
534     if (IDVal == ".balignw")
535       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
536     if (IDVal == ".balignl")
537       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
538     if (IDVal == ".p2align")
539       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
540     if (IDVal == ".p2alignw")
541       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
542     if (IDVal == ".p2alignl")
543       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
544
545     if (IDVal == ".org")
546       return ParseDirectiveOrg();
547
548     if (IDVal == ".fill")
549       return ParseDirectiveFill();
550     if (IDVal == ".space")
551       return ParseDirectiveSpace();
552
553     // Symbol attribute directives
554
555     if (IDVal == ".globl" || IDVal == ".global")
556       return ParseDirectiveSymbolAttribute(MCStreamer::Global);
557     if (IDVal == ".hidden")
558       return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
559     if (IDVal == ".indirect_symbol")
560       return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
561     if (IDVal == ".internal")
562       return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
563     if (IDVal == ".lazy_reference")
564       return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
565     if (IDVal == ".no_dead_strip")
566       return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
567     if (IDVal == ".private_extern")
568       return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
569     if (IDVal == ".protected")
570       return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
571     if (IDVal == ".reference")
572       return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
573     if (IDVal == ".weak")
574       return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
575     if (IDVal == ".weak_definition")
576       return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
577     if (IDVal == ".weak_reference")
578       return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
579
580     if (IDVal == ".comm")
581       return ParseDirectiveComm(/*IsLocal=*/false);
582     if (IDVal == ".lcomm")
583       return ParseDirectiveComm(/*IsLocal=*/true);
584     if (IDVal == ".zerofill")
585       return ParseDirectiveDarwinZerofill();
586     if (IDVal == ".desc")
587       return ParseDirectiveDarwinSymbolDesc();
588     if (IDVal == ".lsym")
589       return ParseDirectiveDarwinLsym();
590
591     if (IDVal == ".subsections_via_symbols")
592       return ParseDirectiveDarwinSubsectionsViaSymbols();
593     if (IDVal == ".abort")
594       return ParseDirectiveAbort();
595     if (IDVal == ".include")
596       return ParseDirectiveInclude();
597     if (IDVal == ".dump")
598       return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
599     if (IDVal == ".load")
600       return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
601
602     // Debugging directives
603
604     if (IDVal == ".file")
605       return ParseDirectiveFile(IDLoc);
606     if (IDVal == ".line")
607       return ParseDirectiveLine(IDLoc);
608     if (IDVal == ".loc")
609       return ParseDirectiveLoc(IDLoc);
610
611     Warning(IDLoc, "ignoring directive for now");
612     EatToEndOfStatement();
613     return false;
614   }
615
616   MCInst Inst;
617   if (getTargetParser().ParseInstruction(IDVal, Inst))
618     return true;
619   
620   if (Lexer.isNot(AsmToken::EndOfStatement))
621     return TokError("unexpected token in argument list");
622
623   // Eat the end of statement marker.
624   Lexer.Lex();
625   
626   // Instruction is good, process it.
627   Out.EmitInstruction(Inst);
628   
629   // Skip to end of line for now.
630   return false;
631 }
632
633 bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
634   // FIXME: Use better location, we should use proper tokens.
635   SMLoc EqualLoc = Lexer.getLoc();
636
637   MCValue Value;
638   if (ParseRelocatableExpression(Value))
639     return true;
640   
641   if (Lexer.isNot(AsmToken::EndOfStatement))
642     return TokError("unexpected token in assignment");
643
644   // Eat the end of statement marker.
645   Lexer.Lex();
646
647   // Diagnose assignment to a label.
648   //
649   // FIXME: Diagnostics. Note the location of the definition as a label.
650   // FIXME: This doesn't diagnose assignment to a symbol which has been
651   // implicitly marked as external.
652   // FIXME: Handle '.'.
653   // FIXME: Diagnose assignment to protected identifier (e.g., register name).
654   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
655   if (Sym->getSection())
656     return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
657   if (Sym->isExternal())
658     return Error(EqualLoc, "invalid assignment to external symbol");
659
660   // Do the assignment.
661   Out.EmitAssignment(Sym, Value, IsDotSet);
662
663   return false;
664 }
665
666 /// ParseIdentifier:
667 ///   ::= identifier
668 ///   ::= string
669 bool AsmParser::ParseIdentifier(StringRef &Res) {
670   if (Lexer.isNot(AsmToken::Identifier) &&
671       Lexer.isNot(AsmToken::String))
672     return true;
673
674   Res = Lexer.getTok().getIdentifier();
675
676   Lexer.Lex(); // Consume the identifier token.
677
678   return false;
679 }
680
681 /// ParseDirectiveSet:
682 ///   ::= .set identifier ',' expression
683 bool AsmParser::ParseDirectiveSet() {
684   StringRef Name;
685
686   if (ParseIdentifier(Name))
687     return TokError("expected identifier after '.set' directive");
688   
689   if (Lexer.isNot(AsmToken::Comma))
690     return TokError("unexpected token in '.set'");
691   Lexer.Lex();
692
693   return ParseAssignment(Name, true);
694 }
695
696 /// ParseDirectiveSection:
697 ///   ::= .section identifier (',' identifier)*
698 /// FIXME: This should actually parse out the segment, section, attributes and
699 /// sizeof_stub fields.
700 bool AsmParser::ParseDirectiveDarwinSection() {
701   SMLoc Loc = Lexer.getLoc();
702
703   StringRef SectionName;
704   if (ParseIdentifier(SectionName))
705     return Error(Loc, "expected identifier after '.section' directive");
706
707   // Verify there is a following comma.
708   if (!Lexer.is(AsmToken::Comma))
709     return TokError("unexpected token in '.section' directive");
710
711   std::string SectionSpec = SectionName;
712   SectionSpec += ",";
713
714   // Add all the tokens until the end of the line, ParseSectionSpecifier will
715   // handle this.
716   StringRef EOL = Lexer.LexUntilEndOfStatement();
717   SectionSpec.append(EOL.begin(), EOL.end());
718
719   Lexer.Lex();
720   if (Lexer.isNot(AsmToken::EndOfStatement))
721     return TokError("unexpected token in '.section' directive");
722   Lexer.Lex();
723
724
725   StringRef Segment, Section;
726   unsigned TAA, StubSize;
727   std::string ErrorStr = 
728     MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
729                                           TAA, StubSize);
730   
731   if (!ErrorStr.empty())
732     return Error(Loc, ErrorStr.c_str());
733   
734   // FIXME: CACHE THESE.
735   
736   // FIXME: Arch specific.
737   MCSection *S = 0; //Ctx.GetSection(Section);
738   if (S == 0)
739     S = MCSectionMachO::Create(Segment, Section, TAA, StubSize,
740                                SectionKind(), Ctx);
741   
742   Out.SwitchSection(S);
743   return false;
744 }
745
746 /// ParseDirectiveSectionSwitch - 
747 ///
748 /// FIXME! Many of these directives implicitly cause a ".align" directive to get
749 /// emitted, we don't do this yet which can lead to subtle miscompiles.
750 bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
751                                             const char *Section,
752                                             unsigned TAA, unsigned StubSize) {
753   if (Lexer.isNot(AsmToken::EndOfStatement))
754     return TokError("unexpected token in section switching directive");
755   Lexer.Lex();
756   
757   // FIXME: Arch specific.
758   // FIXME: Cache this!
759   MCSection *S = 0; // Ctx.GetSection(Section);
760   if (S == 0)
761     S = MCSectionMachO::Create(Segment, Section, TAA, StubSize,
762                                SectionKind(), Ctx);
763   
764   Out.SwitchSection(S);
765   return false;
766 }
767
768 /// ParseDirectiveAscii:
769 ///   ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
770 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
771   if (Lexer.isNot(AsmToken::EndOfStatement)) {
772     for (;;) {
773       if (Lexer.isNot(AsmToken::String))
774         return TokError("expected string in '.ascii' or '.asciz' directive");
775       
776       // FIXME: This shouldn't use a const char* + strlen, the string could have
777       // embedded nulls.
778       // FIXME: Should have accessor for getting string contents.
779       StringRef Str = Lexer.getTok().getString();
780       Out.EmitBytes(Str.substr(1, Str.size() - 2));
781       if (ZeroTerminated)
782         Out.EmitBytes(StringRef("\0", 1));
783       
784       Lexer.Lex();
785       
786       if (Lexer.is(AsmToken::EndOfStatement))
787         break;
788
789       if (Lexer.isNot(AsmToken::Comma))
790         return TokError("unexpected token in '.ascii' or '.asciz' directive");
791       Lexer.Lex();
792     }
793   }
794
795   Lexer.Lex();
796   return false;
797 }
798
799 /// ParseDirectiveValue
800 ///  ::= (.byte | .short | ... ) [ expression (, expression)* ]
801 bool AsmParser::ParseDirectiveValue(unsigned Size) {
802   if (Lexer.isNot(AsmToken::EndOfStatement)) {
803     for (;;) {
804       MCValue Expr;
805       if (ParseRelocatableExpression(Expr))
806         return true;
807
808       Out.EmitValue(Expr, Size);
809
810       if (Lexer.is(AsmToken::EndOfStatement))
811         break;
812       
813       // FIXME: Improve diagnostic.
814       if (Lexer.isNot(AsmToken::Comma))
815         return TokError("unexpected token in directive");
816       Lexer.Lex();
817     }
818   }
819
820   Lexer.Lex();
821   return false;
822 }
823
824 /// ParseDirectiveSpace
825 ///  ::= .space expression [ , expression ]
826 bool AsmParser::ParseDirectiveSpace() {
827   int64_t NumBytes;
828   if (ParseAbsoluteExpression(NumBytes))
829     return true;
830
831   int64_t FillExpr = 0;
832   bool HasFillExpr = false;
833   if (Lexer.isNot(AsmToken::EndOfStatement)) {
834     if (Lexer.isNot(AsmToken::Comma))
835       return TokError("unexpected token in '.space' directive");
836     Lexer.Lex();
837     
838     if (ParseAbsoluteExpression(FillExpr))
839       return true;
840
841     HasFillExpr = true;
842
843     if (Lexer.isNot(AsmToken::EndOfStatement))
844       return TokError("unexpected token in '.space' directive");
845   }
846
847   Lexer.Lex();
848
849   if (NumBytes <= 0)
850     return TokError("invalid number of bytes in '.space' directive");
851
852   // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
853   for (uint64_t i = 0, e = NumBytes; i != e; ++i)
854     Out.EmitValue(MCValue::get(FillExpr), 1);
855
856   return false;
857 }
858
859 /// ParseDirectiveFill
860 ///  ::= .fill expression , expression , expression
861 bool AsmParser::ParseDirectiveFill() {
862   int64_t NumValues;
863   if (ParseAbsoluteExpression(NumValues))
864     return true;
865
866   if (Lexer.isNot(AsmToken::Comma))
867     return TokError("unexpected token in '.fill' directive");
868   Lexer.Lex();
869   
870   int64_t FillSize;
871   if (ParseAbsoluteExpression(FillSize))
872     return true;
873
874   if (Lexer.isNot(AsmToken::Comma))
875     return TokError("unexpected token in '.fill' directive");
876   Lexer.Lex();
877   
878   int64_t FillExpr;
879   if (ParseAbsoluteExpression(FillExpr))
880     return true;
881
882   if (Lexer.isNot(AsmToken::EndOfStatement))
883     return TokError("unexpected token in '.fill' directive");
884   
885   Lexer.Lex();
886
887   if (FillSize != 1 && FillSize != 2 && FillSize != 4)
888     return TokError("invalid '.fill' size, expected 1, 2, or 4");
889
890   for (uint64_t i = 0, e = NumValues; i != e; ++i)
891     Out.EmitValue(MCValue::get(FillExpr), FillSize);
892
893   return false;
894 }
895
896 /// ParseDirectiveOrg
897 ///  ::= .org expression [ , expression ]
898 bool AsmParser::ParseDirectiveOrg() {
899   MCValue Offset;
900   if (ParseRelocatableExpression(Offset))
901     return true;
902
903   // Parse optional fill expression.
904   int64_t FillExpr = 0;
905   if (Lexer.isNot(AsmToken::EndOfStatement)) {
906     if (Lexer.isNot(AsmToken::Comma))
907       return TokError("unexpected token in '.org' directive");
908     Lexer.Lex();
909     
910     if (ParseAbsoluteExpression(FillExpr))
911       return true;
912
913     if (Lexer.isNot(AsmToken::EndOfStatement))
914       return TokError("unexpected token in '.org' directive");
915   }
916
917   Lexer.Lex();
918
919   // FIXME: Only limited forms of relocatable expressions are accepted here, it
920   // has to be relative to the current section.
921   Out.EmitValueToOffset(Offset, FillExpr);
922
923   return false;
924 }
925
926 /// ParseDirectiveAlign
927 ///  ::= {.align, ...} expression [ , expression [ , expression ]]
928 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
929   int64_t Alignment;
930   if (ParseAbsoluteExpression(Alignment))
931     return true;
932
933   SMLoc MaxBytesLoc;
934   bool HasFillExpr = false;
935   int64_t FillExpr = 0;
936   int64_t MaxBytesToFill = 0;
937   if (Lexer.isNot(AsmToken::EndOfStatement)) {
938     if (Lexer.isNot(AsmToken::Comma))
939       return TokError("unexpected token in directive");
940     Lexer.Lex();
941
942     // The fill expression can be omitted while specifying a maximum number of
943     // alignment bytes, e.g:
944     //  .align 3,,4
945     if (Lexer.isNot(AsmToken::Comma)) {
946       HasFillExpr = true;
947       if (ParseAbsoluteExpression(FillExpr))
948         return true;
949     }
950
951     if (Lexer.isNot(AsmToken::EndOfStatement)) {
952       if (Lexer.isNot(AsmToken::Comma))
953         return TokError("unexpected token in directive");
954       Lexer.Lex();
955
956       MaxBytesLoc = Lexer.getLoc();
957       if (ParseAbsoluteExpression(MaxBytesToFill))
958         return true;
959       
960       if (Lexer.isNot(AsmToken::EndOfStatement))
961         return TokError("unexpected token in directive");
962     }
963   }
964
965   Lexer.Lex();
966
967   if (!HasFillExpr) {
968     // FIXME: Sometimes fill with nop.
969     FillExpr = 0;
970   }
971
972   // Compute alignment in bytes.
973   if (IsPow2) {
974     // FIXME: Diagnose overflow.
975     Alignment = 1LL << Alignment;
976   }
977
978   // Diagnose non-sensical max bytes to fill.
979   if (MaxBytesLoc.isValid()) {
980     if (MaxBytesToFill < 1) {
981       Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
982               "many bytes, ignoring");
983       return false;
984     }
985
986     if (MaxBytesToFill >= Alignment) {
987       Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
988               "has no effect");
989       MaxBytesToFill = 0;
990     }
991   }
992
993   // FIXME: Target specific behavior about how the "extra" bytes are filled.
994   Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
995
996   return false;
997 }
998
999 /// ParseDirectiveSymbolAttribute
1000 ///  ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1001 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
1002   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1003     for (;;) {
1004       StringRef Name;
1005
1006       if (ParseIdentifier(Name))
1007         return TokError("expected identifier in directive");
1008       
1009       MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
1010
1011       // If this is use of an undefined symbol then mark it external.
1012       if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
1013         Sym->setExternal(true);
1014
1015       Out.EmitSymbolAttribute(Sym, Attr);
1016
1017       if (Lexer.is(AsmToken::EndOfStatement))
1018         break;
1019
1020       if (Lexer.isNot(AsmToken::Comma))
1021         return TokError("unexpected token in directive");
1022       Lexer.Lex();
1023     }
1024   }
1025
1026   Lexer.Lex();
1027   return false;  
1028 }
1029
1030 /// ParseDirectiveDarwinSymbolDesc
1031 ///  ::= .desc identifier , expression
1032 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
1033   StringRef Name;
1034   if (ParseIdentifier(Name))
1035     return TokError("expected identifier in directive");
1036   
1037   // Handle the identifier as the key symbol.
1038   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
1039
1040   if (Lexer.isNot(AsmToken::Comma))
1041     return TokError("unexpected token in '.desc' directive");
1042   Lexer.Lex();
1043
1044   SMLoc DescLoc = Lexer.getLoc();
1045   int64_t DescValue;
1046   if (ParseAbsoluteExpression(DescValue))
1047     return true;
1048
1049   if (Lexer.isNot(AsmToken::EndOfStatement))
1050     return TokError("unexpected token in '.desc' directive");
1051   
1052   Lexer.Lex();
1053
1054   // Set the n_desc field of this Symbol to this DescValue
1055   Out.EmitSymbolDesc(Sym, DescValue);
1056
1057   return false;
1058 }
1059
1060 /// ParseDirectiveComm
1061 ///  ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1062 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1063   SMLoc IDLoc = Lexer.getLoc();
1064   StringRef Name;
1065   if (ParseIdentifier(Name))
1066     return TokError("expected identifier in directive");
1067   
1068   // Handle the identifier as the key symbol.
1069   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
1070
1071   if (Lexer.isNot(AsmToken::Comma))
1072     return TokError("unexpected token in directive");
1073   Lexer.Lex();
1074
1075   int64_t Size;
1076   SMLoc SizeLoc = Lexer.getLoc();
1077   if (ParseAbsoluteExpression(Size))
1078     return true;
1079
1080   int64_t Pow2Alignment = 0;
1081   SMLoc Pow2AlignmentLoc;
1082   if (Lexer.is(AsmToken::Comma)) {
1083     Lexer.Lex();
1084     Pow2AlignmentLoc = Lexer.getLoc();
1085     if (ParseAbsoluteExpression(Pow2Alignment))
1086       return true;
1087   }
1088   
1089   if (Lexer.isNot(AsmToken::EndOfStatement))
1090     return TokError("unexpected token in '.comm' or '.lcomm' directive");
1091   
1092   Lexer.Lex();
1093
1094   // NOTE: a size of zero for a .comm should create a undefined symbol
1095   // but a size of .lcomm creates a bss symbol of size zero.
1096   if (Size < 0)
1097     return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1098                  "be less than zero");
1099
1100   // NOTE: The alignment in the directive is a power of 2 value, the assember
1101   // may internally end up wanting an alignment in bytes.
1102   // FIXME: Diagnose overflow.
1103   if (Pow2Alignment < 0)
1104     return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1105                  "alignment, can't be less than zero");
1106
1107   // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1108   if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1109     return Error(IDLoc, "invalid symbol redefinition");
1110
1111   // Create the Symbol as a common or local common with Size and Pow2Alignment
1112   Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
1113
1114   return false;
1115 }
1116
1117 /// ParseDirectiveDarwinZerofill
1118 ///  ::= .zerofill segname , sectname [, identifier , size_expression [
1119 ///      , align_expression ]]
1120 bool AsmParser::ParseDirectiveDarwinZerofill() {
1121   // FIXME: Handle quoted names here.
1122
1123   if (Lexer.isNot(AsmToken::Identifier))
1124     return TokError("expected segment name after '.zerofill' directive");
1125   StringRef Segment = Lexer.getTok().getString();
1126   Lexer.Lex();
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 section name after comma in '.zerofill' "
1134                     "directive");
1135   StringRef Section = Lexer.getTok().getString();
1136   Lexer.Lex();
1137
1138   // If this is the end of the line all that was wanted was to create the
1139   // the section but with no symbol.
1140   if (Lexer.is(AsmToken::EndOfStatement)) {
1141     // FIXME: CACHE THIS.
1142     MCSection *S = 0; //Ctx.GetSection(Section);
1143     if (S == 0)
1144       S = MCSectionMachO::Create(Segment, Section,
1145                                  MCSectionMachO::S_ZEROFILL, 0,
1146                                  SectionKind(), Ctx);
1147     
1148     // Create the zerofill section but no symbol
1149     Out.EmitZerofill(S);
1150     return false;
1151   }
1152
1153   if (Lexer.isNot(AsmToken::Comma))
1154     return TokError("unexpected token in directive");
1155   Lexer.Lex();
1156
1157   if (Lexer.isNot(AsmToken::Identifier))
1158     return TokError("expected identifier in directive");
1159   
1160   // handle the identifier as the key symbol.
1161   SMLoc IDLoc = Lexer.getLoc();
1162   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
1163   Lexer.Lex();
1164
1165   if (Lexer.isNot(AsmToken::Comma))
1166     return TokError("unexpected token in directive");
1167   Lexer.Lex();
1168
1169   int64_t Size;
1170   SMLoc SizeLoc = Lexer.getLoc();
1171   if (ParseAbsoluteExpression(Size))
1172     return true;
1173
1174   int64_t Pow2Alignment = 0;
1175   SMLoc Pow2AlignmentLoc;
1176   if (Lexer.is(AsmToken::Comma)) {
1177     Lexer.Lex();
1178     Pow2AlignmentLoc = Lexer.getLoc();
1179     if (ParseAbsoluteExpression(Pow2Alignment))
1180       return true;
1181   }
1182   
1183   if (Lexer.isNot(AsmToken::EndOfStatement))
1184     return TokError("unexpected token in '.zerofill' directive");
1185   
1186   Lexer.Lex();
1187
1188   if (Size < 0)
1189     return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1190                  "than zero");
1191
1192   // NOTE: The alignment in the directive is a power of 2 value, the assember
1193   // may internally end up wanting an alignment in bytes.
1194   // FIXME: Diagnose overflow.
1195   if (Pow2Alignment < 0)
1196     return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1197                  "can't be less than zero");
1198
1199   // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1200   if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1201     return Error(IDLoc, "invalid symbol redefinition");
1202
1203   // FIXME: Arch specific.
1204   // FIXME: CACHE.
1205   MCSection *S = 0; //Ctx.GetSection(Section);
1206   if (S == 0)
1207     S = MCSectionMachO::Create(Segment, Section,
1208                                MCSectionMachO::S_ZEROFILL, 0,
1209                                SectionKind(), Ctx);
1210   
1211   // Create the zerofill Symbol with Size and Pow2Alignment
1212   Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
1213
1214   return false;
1215 }
1216
1217 /// ParseDirectiveDarwinSubsectionsViaSymbols
1218 ///  ::= .subsections_via_symbols
1219 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1220   if (Lexer.isNot(AsmToken::EndOfStatement))
1221     return TokError("unexpected token in '.subsections_via_symbols' directive");
1222   
1223   Lexer.Lex();
1224
1225   Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1226
1227   return false;
1228 }
1229
1230 /// ParseDirectiveAbort
1231 ///  ::= .abort [ "abort_string" ]
1232 bool AsmParser::ParseDirectiveAbort() {
1233   // FIXME: Use loc from directive.
1234   SMLoc Loc = Lexer.getLoc();
1235
1236   StringRef Str = "";
1237   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1238     if (Lexer.isNot(AsmToken::String))
1239       return TokError("expected string in '.abort' directive");
1240     
1241     Str = Lexer.getTok().getString();
1242
1243     Lexer.Lex();
1244   }
1245
1246   if (Lexer.isNot(AsmToken::EndOfStatement))
1247     return TokError("unexpected token in '.abort' directive");
1248   
1249   Lexer.Lex();
1250
1251   // FIXME: Handle here.
1252   if (Str.empty())
1253     Error(Loc, ".abort detected. Assembly stopping.");
1254   else
1255     Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1256
1257   return false;
1258 }
1259
1260 /// ParseDirectiveLsym
1261 ///  ::= .lsym identifier , expression
1262 bool AsmParser::ParseDirectiveDarwinLsym() {
1263   StringRef Name;
1264   if (ParseIdentifier(Name))
1265     return TokError("expected identifier in directive");
1266   
1267   // Handle the identifier as the key symbol.
1268   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
1269
1270   if (Lexer.isNot(AsmToken::Comma))
1271     return TokError("unexpected token in '.lsym' directive");
1272   Lexer.Lex();
1273
1274   MCValue Expr;
1275   if (ParseRelocatableExpression(Expr))
1276     return true;
1277
1278   if (Lexer.isNot(AsmToken::EndOfStatement))
1279     return TokError("unexpected token in '.lsym' directive");
1280   
1281   Lexer.Lex();
1282
1283   // Create the Sym with the value of the Expr
1284   Out.EmitLocalSymbol(Sym, Expr);
1285
1286   return false;
1287 }
1288
1289 /// ParseDirectiveInclude
1290 ///  ::= .include "filename"
1291 bool AsmParser::ParseDirectiveInclude() {
1292   if (Lexer.isNot(AsmToken::String))
1293     return TokError("expected string in '.include' directive");
1294   
1295   std::string Filename = Lexer.getTok().getString();
1296   SMLoc IncludeLoc = Lexer.getLoc();
1297   Lexer.Lex();
1298
1299   if (Lexer.isNot(AsmToken::EndOfStatement))
1300     return TokError("unexpected token in '.include' directive");
1301   
1302   // Strip the quotes.
1303   Filename = Filename.substr(1, Filename.size()-2);
1304   
1305   // Attempt to switch the lexer to the included file before consuming the end
1306   // of statement to avoid losing it when we switch.
1307   if (Lexer.EnterIncludeFile(Filename)) {
1308     Lexer.PrintMessage(IncludeLoc,
1309                        "Could not find include file '" + Filename + "'",
1310                        "error");
1311     return true;
1312   }
1313
1314   return false;
1315 }
1316
1317 /// ParseDirectiveDarwinDumpOrLoad
1318 ///  ::= ( .dump | .load ) "filename"
1319 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1320   if (Lexer.isNot(AsmToken::String))
1321     return TokError("expected string in '.dump' or '.load' directive");
1322   
1323   Lexer.Lex();
1324
1325   if (Lexer.isNot(AsmToken::EndOfStatement))
1326     return TokError("unexpected token in '.dump' or '.load' directive");
1327   
1328   Lexer.Lex();
1329
1330   // FIXME: If/when .dump and .load are implemented they will be done in the
1331   // the assembly parser and not have any need for an MCStreamer API.
1332   if (IsDump)
1333     Warning(IDLoc, "ignoring directive .dump for now");
1334   else
1335     Warning(IDLoc, "ignoring directive .load for now");
1336
1337   return false;
1338 }
1339
1340 /// ParseDirectiveIf
1341 /// ::= .if expression
1342 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1343   // Consume the identifier that was the .if directive
1344   Lexer.Lex();
1345
1346   TheCondStack.push_back(TheCondState);
1347   TheCondState.TheCond = AsmCond::IfCond;
1348   if(TheCondState.Ignore) {
1349     EatToEndOfStatement();
1350   }
1351   else {
1352     int64_t ExprValue;
1353     if (ParseAbsoluteExpression(ExprValue))
1354       return true;
1355
1356     if (Lexer.isNot(AsmToken::EndOfStatement))
1357       return TokError("unexpected token in '.if' directive");
1358     
1359     Lexer.Lex();
1360
1361     TheCondState.CondMet = ExprValue;
1362     TheCondState.Ignore = !TheCondState.CondMet;
1363   }
1364
1365   return false;
1366 }
1367
1368 /// ParseDirectiveElseIf
1369 /// ::= .elseif expression
1370 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1371   if (TheCondState.TheCond != AsmCond::IfCond &&
1372       TheCondState.TheCond != AsmCond::ElseIfCond)
1373       Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1374                           " an .elseif");
1375   TheCondState.TheCond = AsmCond::ElseIfCond;
1376
1377   // Consume the identifier that was the .elseif directive
1378   Lexer.Lex();
1379
1380   bool LastIgnoreState = false;
1381   if (!TheCondStack.empty())
1382       LastIgnoreState = TheCondStack.back().Ignore;
1383   if (LastIgnoreState || TheCondState.CondMet) {
1384     TheCondState.Ignore = true;
1385     EatToEndOfStatement();
1386   }
1387   else {
1388     int64_t ExprValue;
1389     if (ParseAbsoluteExpression(ExprValue))
1390       return true;
1391
1392     if (Lexer.isNot(AsmToken::EndOfStatement))
1393       return TokError("unexpected token in '.elseif' directive");
1394     
1395     Lexer.Lex();
1396     TheCondState.CondMet = ExprValue;
1397     TheCondState.Ignore = !TheCondState.CondMet;
1398   }
1399
1400   return false;
1401 }
1402
1403 /// ParseDirectiveElse
1404 /// ::= .else
1405 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1406   // Consume the identifier that was the .else directive
1407   Lexer.Lex();
1408
1409   if (Lexer.isNot(AsmToken::EndOfStatement))
1410     return TokError("unexpected token in '.else' directive");
1411   
1412   Lexer.Lex();
1413
1414   if (TheCondState.TheCond != AsmCond::IfCond &&
1415       TheCondState.TheCond != AsmCond::ElseIfCond)
1416       Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1417                           ".elseif");
1418   TheCondState.TheCond = AsmCond::ElseCond;
1419   bool LastIgnoreState = false;
1420   if (!TheCondStack.empty())
1421     LastIgnoreState = TheCondStack.back().Ignore;
1422   if (LastIgnoreState || TheCondState.CondMet)
1423     TheCondState.Ignore = true;
1424   else
1425     TheCondState.Ignore = false;
1426
1427   return false;
1428 }
1429
1430 /// ParseDirectiveEndIf
1431 /// ::= .endif
1432 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1433   // Consume the identifier that was the .endif directive
1434   Lexer.Lex();
1435
1436   if (Lexer.isNot(AsmToken::EndOfStatement))
1437     return TokError("unexpected token in '.endif' directive");
1438   
1439   Lexer.Lex();
1440
1441   if ((TheCondState.TheCond == AsmCond::NoCond) ||
1442       TheCondStack.empty())
1443     Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1444                         ".else");
1445   if (!TheCondStack.empty()) {
1446     TheCondState = TheCondStack.back();
1447     TheCondStack.pop_back();
1448   }
1449
1450   return false;
1451 }
1452
1453 /// ParseDirectiveFile
1454 /// ::= .file [number] string
1455 bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) {
1456   // FIXME: I'm not sure what this is.
1457   int64_t FileNumber = -1;
1458   if (Lexer.is(AsmToken::Integer)) {
1459     FileNumber = Lexer.getTok().getIntVal();
1460     Lexer.Lex();
1461     
1462     if (FileNumber < 1)
1463       return TokError("file number less than one");
1464   }
1465
1466   if (Lexer.isNot(AsmToken::String))
1467     return TokError("unexpected token in '.file' directive");
1468   
1469   StringRef FileName = Lexer.getTok().getString();
1470   Lexer.Lex();
1471
1472   if (Lexer.isNot(AsmToken::EndOfStatement))
1473     return TokError("unexpected token in '.file' directive");
1474
1475   // FIXME: Do something with the .file.
1476
1477   return false;
1478 }
1479
1480 /// ParseDirectiveLine
1481 /// ::= .line [number]
1482 bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) {
1483   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1484     if (Lexer.isNot(AsmToken::Integer))
1485       return TokError("unexpected token in '.line' directive");
1486
1487     int64_t LineNumber = Lexer.getTok().getIntVal();
1488     (void) LineNumber;
1489     Lexer.Lex();
1490
1491     // FIXME: Do something with the .line.
1492   }
1493
1494   if (Lexer.isNot(AsmToken::EndOfStatement))
1495     return TokError("unexpected token in '.file' directive");
1496
1497   return false;
1498 }
1499
1500
1501 /// ParseDirectiveLoc
1502 /// ::= .loc number [number [number]]
1503 bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) {
1504   if (Lexer.isNot(AsmToken::Integer))
1505     return TokError("unexpected token in '.loc' directive");
1506
1507   // FIXME: What are these fields?
1508   int64_t FileNumber = Lexer.getTok().getIntVal();
1509   (void) FileNumber;
1510   // FIXME: Validate file.
1511
1512   Lexer.Lex();
1513   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1514     if (Lexer.isNot(AsmToken::Integer))
1515       return TokError("unexpected token in '.loc' directive");
1516
1517     int64_t Param2 = Lexer.getTok().getIntVal();
1518     (void) Param2;
1519     Lexer.Lex();
1520
1521     if (Lexer.isNot(AsmToken::EndOfStatement)) {
1522       if (Lexer.isNot(AsmToken::Integer))
1523         return TokError("unexpected token in '.loc' directive");
1524
1525       int64_t Param3 = Lexer.getTok().getIntVal();
1526       (void) Param3;
1527       Lexer.Lex();
1528
1529       // FIXME: Do something with the .loc.
1530     }
1531   }
1532
1533   if (Lexer.isNot(AsmToken::EndOfStatement))
1534     return TokError("unexpected token in '.file' directive");
1535
1536   return false;
1537 }
1538