Normalize SourceMgr messages.
[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/MC/MCContext.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm;
24
25 void AsmParser::Warning(SMLoc L, const char *Msg) {
26   Lexer.PrintMessage(L, Msg, "warning");
27 }
28
29 bool AsmParser::Error(SMLoc L, const char *Msg) {
30   Lexer.PrintMessage(L, Msg, "error");
31   return true;
32 }
33
34 bool AsmParser::TokError(const char *Msg) {
35   Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
36   return true;
37 }
38
39 bool AsmParser::Run() {
40   // Prime the lexer.
41   Lexer.Lex();
42   
43   while (Lexer.isNot(asmtok::Eof))
44     if (ParseStatement())
45       return true;
46   
47   return false;
48 }
49
50 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
51 void AsmParser::EatToEndOfStatement() {
52   while (Lexer.isNot(asmtok::EndOfStatement) &&
53          Lexer.isNot(asmtok::Eof))
54     Lexer.Lex();
55   
56   // Eat EOL.
57   if (Lexer.is(asmtok::EndOfStatement))
58     Lexer.Lex();
59 }
60
61
62 /// ParseParenExpr - Parse a paren expression and return it.
63 /// NOTE: This assumes the leading '(' has already been consumed.
64 ///
65 /// parenexpr ::= expr)
66 ///
67 bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
68   if (ParseExpression(Res)) return true;
69   if (Lexer.isNot(asmtok::RParen))
70     return TokError("expected ')' in parentheses expression");
71   Lexer.Lex();
72   return false;
73 }
74
75 /// ParsePrimaryExpr - Parse a primary expression and return it.
76 ///  primaryexpr ::= (parenexpr
77 ///  primaryexpr ::= symbol
78 ///  primaryexpr ::= number
79 ///  primaryexpr ::= ~,+,- primaryexpr
80 bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
81   switch (Lexer.getKind()) {
82   default:
83     return TokError("unknown token in expression");
84   case asmtok::Exclaim:
85     Lexer.Lex(); // Eat the operator.
86     if (ParsePrimaryExpr(Res))
87       return true;
88     Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
89     return false;
90   case asmtok::Identifier: {
91     // This is a label, this should be parsed as part of an expression, to
92     // handle things like LFOO+4.
93     MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
94
95     // If this is use of an undefined symbol then mark it external.
96     if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
97       Sym->setExternal(true);
98     
99     Res = new AsmSymbolRefExpr(Sym);
100     Lexer.Lex(); // Eat identifier.
101     return false;
102   }
103   case asmtok::IntVal:
104     Res = new AsmConstantExpr(Lexer.getCurIntVal());
105     Lexer.Lex(); // Eat identifier.
106     return false;
107   case asmtok::LParen:
108     Lexer.Lex(); // Eat the '('.
109     return ParseParenExpr(Res);
110   case asmtok::Minus:
111     Lexer.Lex(); // Eat the operator.
112     if (ParsePrimaryExpr(Res))
113       return true;
114     Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
115     return false;
116   case asmtok::Plus:
117     Lexer.Lex(); // Eat the operator.
118     if (ParsePrimaryExpr(Res))
119       return true;
120     Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
121     return false;
122   case asmtok::Tilde:
123     Lexer.Lex(); // Eat the operator.
124     if (ParsePrimaryExpr(Res))
125       return true;
126     Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
127     return false;
128   }
129 }
130
131 /// ParseExpression - Parse an expression and return it.
132 /// 
133 ///  expr ::= expr +,- expr          -> lowest.
134 ///  expr ::= expr |,^,&,! expr      -> middle.
135 ///  expr ::= expr *,/,%,<<,>> expr  -> highest.
136 ///  expr ::= primaryexpr
137 ///
138 bool AsmParser::ParseExpression(AsmExpr *&Res) {
139   Res = 0;
140   return ParsePrimaryExpr(Res) ||
141          ParseBinOpRHS(1, Res);
142 }
143
144 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
145   AsmExpr *Expr;
146   
147   if (ParseExpression(Expr))
148     return true;
149
150   if (!Expr->EvaluateAsAbsolute(Ctx, Res))
151     return TokError("expected absolute expression");
152
153   return false;
154 }
155
156 static unsigned getBinOpPrecedence(asmtok::TokKind K, 
157                                    AsmBinaryExpr::Opcode &Kind) {
158   switch (K) {
159   default: return 0;    // not a binop.
160
161     // Lowest Precedence: &&, ||
162   case asmtok::AmpAmp:
163     Kind = AsmBinaryExpr::LAnd;
164     return 1;
165   case asmtok::PipePipe:
166     Kind = AsmBinaryExpr::LOr;
167     return 1;
168
169     // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
170   case asmtok::Plus:
171     Kind = AsmBinaryExpr::Add;
172     return 2;
173   case asmtok::Minus:
174     Kind = AsmBinaryExpr::Sub;
175     return 2;
176   case asmtok::EqualEqual:
177     Kind = AsmBinaryExpr::EQ;
178     return 2;
179   case asmtok::ExclaimEqual:
180   case asmtok::LessGreater:
181     Kind = AsmBinaryExpr::NE;
182     return 2;
183   case asmtok::Less:
184     Kind = AsmBinaryExpr::LT;
185     return 2;
186   case asmtok::LessEqual:
187     Kind = AsmBinaryExpr::LTE;
188     return 2;
189   case asmtok::Greater:
190     Kind = AsmBinaryExpr::GT;
191     return 2;
192   case asmtok::GreaterEqual:
193     Kind = AsmBinaryExpr::GTE;
194     return 2;
195
196     // Intermediate Precedence: |, &, ^
197     //
198     // FIXME: gas seems to support '!' as an infix operator?
199   case asmtok::Pipe:
200     Kind = AsmBinaryExpr::Or;
201     return 3;
202   case asmtok::Caret:
203     Kind = AsmBinaryExpr::Xor;
204     return 3;
205   case asmtok::Amp:
206     Kind = AsmBinaryExpr::And;
207     return 3;
208
209     // Highest Precedence: *, /, %, <<, >>
210   case asmtok::Star:
211     Kind = AsmBinaryExpr::Mul;
212     return 4;
213   case asmtok::Slash:
214     Kind = AsmBinaryExpr::Div;
215     return 4;
216   case asmtok::Percent:
217     Kind = AsmBinaryExpr::Mod;
218     return 4;
219   case asmtok::LessLess:
220     Kind = AsmBinaryExpr::Shl;
221     return 4;
222   case asmtok::GreaterGreater:
223     Kind = AsmBinaryExpr::Shr;
224     return 4;
225   }
226 }
227
228
229 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
230 /// Res contains the LHS of the expression on input.
231 bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
232   while (1) {
233     AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
234     unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
235     
236     // If the next token is lower precedence than we are allowed to eat, return
237     // successfully with what we ate already.
238     if (TokPrec < Precedence)
239       return false;
240     
241     Lexer.Lex();
242     
243     // Eat the next primary expression.
244     AsmExpr *RHS;
245     if (ParsePrimaryExpr(RHS)) return true;
246     
247     // If BinOp binds less tightly with RHS than the operator after RHS, let
248     // the pending operator take RHS as its LHS.
249     AsmBinaryExpr::Opcode Dummy;
250     unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
251     if (TokPrec < NextTokPrec) {
252       if (ParseBinOpRHS(Precedence+1, RHS)) return true;
253     }
254
255     // Merge LHS and RHS according to operator.
256     Res = new AsmBinaryExpr(Kind, Res, RHS);
257   }
258 }
259
260   
261   
262   
263 /// ParseStatement:
264 ///   ::= EndOfStatement
265 ///   ::= Label* Directive ...Operands... EndOfStatement
266 ///   ::= Label* Identifier OperandList* EndOfStatement
267 bool AsmParser::ParseStatement() {
268   switch (Lexer.getKind()) {
269   default:
270     return TokError("unexpected token at start of statement");
271   case asmtok::EndOfStatement:
272     Lexer.Lex();
273     return false;
274   case asmtok::Identifier:
275     break;
276   // TODO: Recurse on local labels etc.
277   }
278   
279   // If we have an identifier, handle it as the key symbol.
280   SMLoc IDLoc = Lexer.getLoc();
281   const char *IDVal = Lexer.getCurStrVal();
282   
283   // Consume the identifier, see what is after it.
284   switch (Lexer.Lex()) {
285   case asmtok::Colon: {
286     // identifier ':'   -> Label.
287     Lexer.Lex();
288
289     // Diagnose attempt to use a variable as a label.
290     //
291     // FIXME: Diagnostics. Note the location of the definition as a label.
292     // FIXME: This doesn't diagnose assignment to a symbol which has been
293     // implicitly marked as external.
294     MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
295     if (Sym->getSection())
296       return Error(IDLoc, "invalid symbol redefinition");
297     if (Ctx.GetSymbolValue(Sym))
298       return Error(IDLoc, "symbol already used as assembler variable");
299     
300     // Since we saw a label, create a symbol and emit it.
301     // FIXME: If the label starts with L it is an assembler temporary label.
302     // Why does the client of this api need to know this?
303     Out.EmitLabel(Sym);
304    
305     return ParseStatement();
306   }
307
308   case asmtok::Equal:
309     // identifier '=' ... -> assignment statement
310     Lexer.Lex();
311
312     return ParseAssignment(IDVal, false);
313
314   default: // Normal instruction or directive.
315     break;
316   }
317   
318   // Otherwise, we have a normal instruction or directive.  
319   if (IDVal[0] == '.') {
320     // FIXME: This should be driven based on a hash lookup and callback.
321     if (!strcmp(IDVal, ".section"))
322       return ParseDirectiveDarwinSection();
323     if (!strcmp(IDVal, ".text"))
324       // FIXME: This changes behavior based on the -static flag to the
325       // assembler.
326       return ParseDirectiveSectionSwitch("__TEXT,__text",
327                                          "regular,pure_instructions");
328     if (!strcmp(IDVal, ".const"))
329       return ParseDirectiveSectionSwitch("__TEXT,__const");
330     if (!strcmp(IDVal, ".static_const"))
331       return ParseDirectiveSectionSwitch("__TEXT,__static_const");
332     if (!strcmp(IDVal, ".cstring"))
333       return ParseDirectiveSectionSwitch("__TEXT,__cstring", 
334                                          "cstring_literals");
335     if (!strcmp(IDVal, ".literal4"))
336       return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
337     if (!strcmp(IDVal, ".literal8"))
338       return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
339     if (!strcmp(IDVal, ".literal16"))
340       return ParseDirectiveSectionSwitch("__TEXT,__literal16",
341                                          "16byte_literals");
342     if (!strcmp(IDVal, ".constructor"))
343       return ParseDirectiveSectionSwitch("__TEXT,__constructor");
344     if (!strcmp(IDVal, ".destructor"))
345       return ParseDirectiveSectionSwitch("__TEXT,__destructor");
346     if (!strcmp(IDVal, ".fvmlib_init0"))
347       return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
348     if (!strcmp(IDVal, ".fvmlib_init1"))
349       return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
350     if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
351       return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
352                                     "self_modifying_code+pure_instructions,5");
353     // FIXME: .picsymbol_stub on PPC.
354     if (!strcmp(IDVal, ".data"))
355       return ParseDirectiveSectionSwitch("__DATA,__data");
356     if (!strcmp(IDVal, ".static_data"))
357       return ParseDirectiveSectionSwitch("__DATA,__static_data");
358     if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
359       return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
360                                          "non_lazy_symbol_pointers");
361     if (!strcmp(IDVal, ".lazy_symbol_pointer"))
362       return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
363                                          "lazy_symbol_pointers");
364     if (!strcmp(IDVal, ".dyld"))
365       return ParseDirectiveSectionSwitch("__DATA,__dyld");
366     if (!strcmp(IDVal, ".mod_init_func"))
367       return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
368                                          "mod_init_funcs");
369     if (!strcmp(IDVal, ".mod_term_func"))
370       return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
371                                          "mod_term_funcs");
372     if (!strcmp(IDVal, ".const_data"))
373       return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
374     
375     
376     // FIXME: Verify attributes on sections.
377     if (!strcmp(IDVal, ".objc_class"))
378       return ParseDirectiveSectionSwitch("__OBJC,__class");
379     if (!strcmp(IDVal, ".objc_meta_class"))
380       return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
381     if (!strcmp(IDVal, ".objc_cat_cls_meth"))
382       return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
383     if (!strcmp(IDVal, ".objc_cat_inst_meth"))
384       return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
385     if (!strcmp(IDVal, ".objc_protocol"))
386       return ParseDirectiveSectionSwitch("__OBJC,__protocol");
387     if (!strcmp(IDVal, ".objc_string_object"))
388       return ParseDirectiveSectionSwitch("__OBJC,__string_object");
389     if (!strcmp(IDVal, ".objc_cls_meth"))
390       return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
391     if (!strcmp(IDVal, ".objc_inst_meth"))
392       return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
393     if (!strcmp(IDVal, ".objc_cls_refs"))
394       return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
395     if (!strcmp(IDVal, ".objc_message_refs"))
396       return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
397     if (!strcmp(IDVal, ".objc_symbols"))
398       return ParseDirectiveSectionSwitch("__OBJC,__symbols");
399     if (!strcmp(IDVal, ".objc_category"))
400       return ParseDirectiveSectionSwitch("__OBJC,__category");
401     if (!strcmp(IDVal, ".objc_class_vars"))
402       return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
403     if (!strcmp(IDVal, ".objc_instance_vars"))
404       return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
405     if (!strcmp(IDVal, ".objc_module_info"))
406       return ParseDirectiveSectionSwitch("__OBJC,__module_info");
407     if (!strcmp(IDVal, ".objc_class_names"))
408       return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
409     if (!strcmp(IDVal, ".objc_meth_var_types"))
410       return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
411     if (!strcmp(IDVal, ".objc_meth_var_names"))
412       return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
413     if (!strcmp(IDVal, ".objc_selector_strs"))
414       return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
415     
416     // Assembler features
417     if (!strcmp(IDVal, ".set"))
418       return ParseDirectiveSet();
419
420     // Data directives
421
422     if (!strcmp(IDVal, ".ascii"))
423       return ParseDirectiveAscii(false);
424     if (!strcmp(IDVal, ".asciz"))
425       return ParseDirectiveAscii(true);
426
427     // FIXME: Target hooks for size? Also for "word", "hword".
428     if (!strcmp(IDVal, ".byte"))
429       return ParseDirectiveValue(1);
430     if (!strcmp(IDVal, ".short"))
431       return ParseDirectiveValue(2);
432     if (!strcmp(IDVal, ".long"))
433       return ParseDirectiveValue(4);
434     if (!strcmp(IDVal, ".quad"))
435       return ParseDirectiveValue(8);
436
437     // FIXME: Target hooks for IsPow2.
438     if (!strcmp(IDVal, ".align"))
439       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
440     if (!strcmp(IDVal, ".align32"))
441       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
442     if (!strcmp(IDVal, ".balign"))
443       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
444     if (!strcmp(IDVal, ".balignw"))
445       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
446     if (!strcmp(IDVal, ".balignl"))
447       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
448     if (!strcmp(IDVal, ".p2align"))
449       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
450     if (!strcmp(IDVal, ".p2alignw"))
451       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
452     if (!strcmp(IDVal, ".p2alignl"))
453       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
454
455     if (!strcmp(IDVal, ".org"))
456       return ParseDirectiveOrg();
457
458     if (!strcmp(IDVal, ".fill"))
459       return ParseDirectiveFill();
460     if (!strcmp(IDVal, ".space"))
461       return ParseDirectiveSpace();
462
463     // Symbol attribute directives
464     if (!strcmp(IDVal, ".globl") || !strcmp(IDVal, ".global"))
465       return ParseDirectiveSymbolAttribute(MCStreamer::Global);
466     if (!strcmp(IDVal, ".hidden"))
467       return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
468     if (!strcmp(IDVal, ".indirect_symbol"))
469       return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
470     if (!strcmp(IDVal, ".internal"))
471       return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
472     if (!strcmp(IDVal, ".lazy_reference"))
473       return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
474     if (!strcmp(IDVal, ".no_dead_strip"))
475       return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
476     if (!strcmp(IDVal, ".private_extern"))
477       return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
478     if (!strcmp(IDVal, ".protected"))
479       return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
480     if (!strcmp(IDVal, ".reference"))
481       return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
482     if (!strcmp(IDVal, ".weak"))
483       return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
484     if (!strcmp(IDVal, ".weak_definition"))
485       return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
486     if (!strcmp(IDVal, ".weak_reference"))
487       return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
488
489     Warning(IDLoc, "ignoring directive for now");
490     EatToEndOfStatement();
491     return false;
492   }
493
494   MCInst Inst;
495   if (ParseX86InstOperands(Inst))
496     return true;
497   
498   if (Lexer.isNot(asmtok::EndOfStatement))
499     return TokError("unexpected token in argument list");
500
501   // Eat the end of statement marker.
502   Lexer.Lex();
503   
504   // Instruction is good, process it.
505   outs() << "Found instruction: " << IDVal << " with " << Inst.getNumOperands()
506          << " operands.\n";
507   
508   // Skip to end of line for now.
509   return false;
510 }
511
512 bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
513   // FIXME: Use better location, we should use proper tokens.
514   SMLoc EqualLoc = Lexer.getLoc();
515
516   int64_t Value;
517   if (ParseAbsoluteExpression(Value))
518     return true;
519   
520   if (Lexer.isNot(asmtok::EndOfStatement))
521     return TokError("unexpected token in assignment");
522
523   // Eat the end of statement marker.
524   Lexer.Lex();
525
526   // Diagnose assignment to a label.
527   //
528   // FIXME: Diagnostics. Note the location of the definition as a label.
529   // FIXME: This doesn't diagnose assignment to a symbol which has been
530   // implicitly marked as external.
531   // FIXME: Handle '.'.
532   // FIXME: Diagnose assignment to protected identifier (e.g., register name).
533   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
534   if (Sym->getSection())
535     return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
536   if (Sym->isExternal())
537     return Error(EqualLoc, "invalid assignment to external symbol");
538
539   // Do the assignment.
540   Out.EmitAssignment(Sym, MCValue::get(Value), IsDotSet);
541
542   return false;
543 }
544
545 /// ParseDirectiveSet:
546 ///   ::= .set identifier ',' expression
547 bool AsmParser::ParseDirectiveSet() {
548   if (Lexer.isNot(asmtok::Identifier))
549     return TokError("expected identifier after '.set' directive");
550
551   const char *Name = Lexer.getCurStrVal();
552   
553   if (Lexer.Lex() != asmtok::Comma)
554     return TokError("unexpected token in '.set'");
555   Lexer.Lex();
556
557   return ParseAssignment(Name, true);
558 }
559
560 /// ParseDirectiveSection:
561 ///   ::= .section identifier (',' identifier)*
562 /// FIXME: This should actually parse out the segment, section, attributes and
563 /// sizeof_stub fields.
564 bool AsmParser::ParseDirectiveDarwinSection() {
565   if (Lexer.isNot(asmtok::Identifier))
566     return TokError("expected identifier after '.section' directive");
567   
568   std::string Section = Lexer.getCurStrVal();
569   Lexer.Lex();
570   
571   // Accept a comma separated list of modifiers.
572   while (Lexer.is(asmtok::Comma)) {
573     Lexer.Lex();
574     
575     if (Lexer.isNot(asmtok::Identifier))
576       return TokError("expected identifier in '.section' directive");
577     Section += ',';
578     Section += Lexer.getCurStrVal();
579     Lexer.Lex();
580   }
581   
582   if (Lexer.isNot(asmtok::EndOfStatement))
583     return TokError("unexpected token in '.section' directive");
584   Lexer.Lex();
585
586   Out.SwitchSection(Ctx.GetSection(Section.c_str()));
587   return false;
588 }
589
590 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
591                                             const char *Directives) {
592   if (Lexer.isNot(asmtok::EndOfStatement))
593     return TokError("unexpected token in section switching directive");
594   Lexer.Lex();
595   
596   std::string SectionStr = Section;
597   if (Directives && Directives[0]) {
598     SectionStr += ","; 
599     SectionStr += Directives;
600   }
601   
602   Out.SwitchSection(Ctx.GetSection(Section));
603   return false;
604 }
605
606 /// ParseDirectiveAscii:
607 ///   ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
608 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
609   if (Lexer.isNot(asmtok::EndOfStatement)) {
610     for (;;) {
611       if (Lexer.isNot(asmtok::String))
612         return TokError("expected string in '.ascii' or '.asciz' directive");
613       
614       // FIXME: This shouldn't use a const char* + strlen, the string could have
615       // embedded nulls.
616       // FIXME: Should have accessor for getting string contents.
617       const char *Str = Lexer.getCurStrVal();
618       Out.EmitBytes(Str + 1, strlen(Str) - 2);
619       if (ZeroTerminated)
620         Out.EmitBytes("\0", 1);
621       
622       Lexer.Lex();
623       
624       if (Lexer.is(asmtok::EndOfStatement))
625         break;
626
627       if (Lexer.isNot(asmtok::Comma))
628         return TokError("unexpected token in '.ascii' or '.asciz' directive");
629       Lexer.Lex();
630     }
631   }
632
633   Lexer.Lex();
634   return false;
635 }
636
637 /// ParseDirectiveValue
638 ///  ::= (.byte | .short | ... ) [ expression (, expression)* ]
639 bool AsmParser::ParseDirectiveValue(unsigned Size) {
640   if (Lexer.isNot(asmtok::EndOfStatement)) {
641     for (;;) {
642       int64_t Expr;
643       if (ParseAbsoluteExpression(Expr))
644         return true;
645
646       Out.EmitValue(MCValue::get(Expr), Size);
647
648       if (Lexer.is(asmtok::EndOfStatement))
649         break;
650       
651       // FIXME: Improve diagnostic.
652       if (Lexer.isNot(asmtok::Comma))
653         return TokError("unexpected token in directive");
654       Lexer.Lex();
655     }
656   }
657
658   Lexer.Lex();
659   return false;
660 }
661
662 /// ParseDirectiveSpace
663 ///  ::= .space expression [ , expression ]
664 bool AsmParser::ParseDirectiveSpace() {
665   int64_t NumBytes;
666   if (ParseAbsoluteExpression(NumBytes))
667     return true;
668
669   int64_t FillExpr = 0;
670   bool HasFillExpr = false;
671   if (Lexer.isNot(asmtok::EndOfStatement)) {
672     if (Lexer.isNot(asmtok::Comma))
673       return TokError("unexpected token in '.space' directive");
674     Lexer.Lex();
675     
676     if (ParseAbsoluteExpression(FillExpr))
677       return true;
678
679     HasFillExpr = true;
680
681     if (Lexer.isNot(asmtok::EndOfStatement))
682       return TokError("unexpected token in '.space' directive");
683   }
684
685   Lexer.Lex();
686
687   if (NumBytes <= 0)
688     return TokError("invalid number of bytes in '.space' directive");
689
690   // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
691   for (uint64_t i = 0, e = NumBytes; i != e; ++i)
692     Out.EmitValue(MCValue::get(FillExpr), 1);
693
694   return false;
695 }
696
697 /// ParseDirectiveFill
698 ///  ::= .fill expression , expression , expression
699 bool AsmParser::ParseDirectiveFill() {
700   int64_t NumValues;
701   if (ParseAbsoluteExpression(NumValues))
702     return true;
703
704   if (Lexer.isNot(asmtok::Comma))
705     return TokError("unexpected token in '.fill' directive");
706   Lexer.Lex();
707   
708   int64_t FillSize;
709   if (ParseAbsoluteExpression(FillSize))
710     return true;
711
712   if (Lexer.isNot(asmtok::Comma))
713     return TokError("unexpected token in '.fill' directive");
714   Lexer.Lex();
715   
716   int64_t FillExpr;
717   if (ParseAbsoluteExpression(FillExpr))
718     return true;
719
720   if (Lexer.isNot(asmtok::EndOfStatement))
721     return TokError("unexpected token in '.fill' directive");
722   
723   Lexer.Lex();
724
725   if (FillSize != 1 && FillSize != 2 && FillSize != 4)
726     return TokError("invalid '.fill' size, expected 1, 2, or 4");
727
728   for (uint64_t i = 0, e = NumValues; i != e; ++i)
729     Out.EmitValue(MCValue::get(FillExpr), FillSize);
730
731   return false;
732 }
733
734 /// ParseDirectiveOrg
735 ///  ::= .org expression [ , expression ]
736 bool AsmParser::ParseDirectiveOrg() {
737   int64_t Offset;
738   if (ParseAbsoluteExpression(Offset))
739     return true;
740
741   // Parse optional fill expression.
742   int64_t FillExpr = 0;
743   if (Lexer.isNot(asmtok::EndOfStatement)) {
744     if (Lexer.isNot(asmtok::Comma))
745       return TokError("unexpected token in '.org' directive");
746     Lexer.Lex();
747     
748     if (ParseAbsoluteExpression(FillExpr))
749       return true;
750
751     if (Lexer.isNot(asmtok::EndOfStatement))
752       return TokError("unexpected token in '.org' directive");
753   }
754
755   Lexer.Lex();
756   
757   Out.EmitValueToOffset(MCValue::get(Offset), FillExpr);
758
759   return false;
760 }
761
762 /// ParseDirectiveAlign
763 ///  ::= {.align, ...} expression [ , expression [ , expression ]]
764 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
765   int64_t Alignment;
766   if (ParseAbsoluteExpression(Alignment))
767     return true;
768
769   SMLoc MaxBytesLoc;
770   bool HasFillExpr = false;
771   int64_t FillExpr = 0;
772   int64_t MaxBytesToFill = 0;
773   if (Lexer.isNot(asmtok::EndOfStatement)) {
774     if (Lexer.isNot(asmtok::Comma))
775       return TokError("unexpected token in directive");
776     Lexer.Lex();
777
778     // The fill expression can be omitted while specifying a maximum number of
779     // alignment bytes, e.g:
780     //  .align 3,,4
781     if (Lexer.isNot(asmtok::Comma)) {
782       HasFillExpr = true;
783       if (ParseAbsoluteExpression(FillExpr))
784         return true;
785     }
786
787     if (Lexer.isNot(asmtok::EndOfStatement)) {
788       if (Lexer.isNot(asmtok::Comma))
789         return TokError("unexpected token in directive");
790       Lexer.Lex();
791
792       MaxBytesLoc = Lexer.getLoc();
793       if (ParseAbsoluteExpression(MaxBytesToFill))
794         return true;
795       
796       if (Lexer.isNot(asmtok::EndOfStatement))
797         return TokError("unexpected token in directive");
798     }
799   }
800
801   Lexer.Lex();
802
803   if (!HasFillExpr) {
804     // FIXME: Sometimes fill with nop.
805     FillExpr = 0;
806   }
807
808   // Compute alignment in bytes.
809   if (IsPow2) {
810     // FIXME: Diagnose overflow.
811     Alignment = 1 << Alignment;
812   }
813
814   // Diagnose non-sensical max bytes to fill.
815   if (MaxBytesLoc.isValid()) {
816     if (MaxBytesToFill < 1) {
817       Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
818               "many bytes, ignoring");
819       return false;
820     }
821
822     if (MaxBytesToFill >= Alignment) {
823       Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
824               "has no effect");
825       MaxBytesToFill = 0;
826     }
827   }
828
829   // FIXME: Target specific behavior about how the "extra" bytes are filled.
830   Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
831
832   return false;
833 }
834
835 /// ParseDirectiveSymbolAttribute
836 ///  ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
837 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
838   if (Lexer.isNot(asmtok::EndOfStatement)) {
839     for (;;) {
840       if (Lexer.isNot(asmtok::Identifier))
841         return TokError("expected identifier in directive");
842       
843       MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
844       Lexer.Lex();
845
846       // If this is use of an undefined symbol then mark it external.
847       if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
848         Sym->setExternal(true);
849
850       Out.EmitSymbolAttribute(Sym, Attr);
851
852       if (Lexer.is(asmtok::EndOfStatement))
853         break;
854
855       if (Lexer.isNot(asmtok::Comma))
856         return TokError("unexpected token in directive");
857       Lexer.Lex();
858     }
859   }
860
861   Lexer.Lex();
862   return false;  
863 }