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