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