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