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