update for API change.
[oota-llvm.git] / tools / llvm-mc / AsmParser.cpp
1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class implements the parser for assembly files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "AsmParser.h"
15
16 #include "AsmExpr.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCSection.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetAsmParser.h"
26 using namespace llvm;
27
28 void AsmParser::Warning(SMLoc L, const Twine &Msg) {
29   Lexer.PrintMessage(L, Msg.str(), "warning");
30 }
31
32 bool AsmParser::Error(SMLoc L, const Twine &Msg) {
33   Lexer.PrintMessage(L, Msg.str(), "error");
34   return true;
35 }
36
37 bool AsmParser::TokError(const char *Msg) {
38   Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
39   return true;
40 }
41
42 bool AsmParser::Run() {
43   // Prime the lexer.
44   Lexer.Lex();
45   
46   bool HadError = false;
47   
48   // While we have input, parse each statement.
49   while (Lexer.isNot(AsmToken::Eof)) {
50     if (!ParseStatement()) continue;
51   
52     // If we had an error, remember it and recover by skipping to the next line.
53     HadError = true;
54     EatToEndOfStatement();
55   }
56   
57   return HadError;
58 }
59
60 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
61 void AsmParser::EatToEndOfStatement() {
62   while (Lexer.isNot(AsmToken::EndOfStatement) &&
63          Lexer.isNot(AsmToken::Eof))
64     Lexer.Lex();
65   
66   // Eat EOL.
67   if (Lexer.is(AsmToken::EndOfStatement))
68     Lexer.Lex();
69 }
70
71
72 /// ParseParenExpr - Parse a paren expression and return it.
73 /// NOTE: This assumes the leading '(' has already been consumed.
74 ///
75 /// parenexpr ::= expr)
76 ///
77 bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
78   if (ParseExpression(Res)) return true;
79   if (Lexer.isNot(AsmToken::RParen))
80     return TokError("expected ')' in parentheses expression");
81   Lexer.Lex();
82   return false;
83 }
84
85 /// ParsePrimaryExpr - Parse a primary expression and return it.
86 ///  primaryexpr ::= (parenexpr
87 ///  primaryexpr ::= symbol
88 ///  primaryexpr ::= number
89 ///  primaryexpr ::= ~,+,- primaryexpr
90 bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
91   switch (Lexer.getKind()) {
92   default:
93     return TokError("unknown token in expression");
94   case AsmToken::Exclaim:
95     Lexer.Lex(); // Eat the operator.
96     if (ParsePrimaryExpr(Res))
97       return true;
98     Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
99     return false;
100   case AsmToken::String:
101   case AsmToken::Identifier: {
102     // This is a label, this should be parsed as part of an expression, to
103     // handle things like LFOO+4.
104     MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getIdentifier());
105
106     // If this is use of an undefined symbol then mark it external.
107     if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
108       Sym->setExternal(true);
109     
110     Res = new AsmSymbolRefExpr(Sym);
111     Lexer.Lex(); // Eat identifier.
112     return false;
113   }
114   case AsmToken::Integer:
115     Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
116     Lexer.Lex(); // Eat token.
117     return false;
118   case AsmToken::LParen:
119     Lexer.Lex(); // Eat the '('.
120     return ParseParenExpr(Res);
121   case AsmToken::Minus:
122     Lexer.Lex(); // Eat the operator.
123     if (ParsePrimaryExpr(Res))
124       return true;
125     Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
126     return false;
127   case AsmToken::Plus:
128     Lexer.Lex(); // Eat the operator.
129     if (ParsePrimaryExpr(Res))
130       return true;
131     Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
132     return false;
133   case AsmToken::Tilde:
134     Lexer.Lex(); // Eat the operator.
135     if (ParsePrimaryExpr(Res))
136       return true;
137     Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
138     return false;
139   }
140 }
141
142 /// ParseExpression - Parse an expression and return it.
143 /// 
144 ///  expr ::= expr +,- expr          -> lowest.
145 ///  expr ::= expr |,^,&,! expr      -> middle.
146 ///  expr ::= expr *,/,%,<<,>> expr  -> highest.
147 ///  expr ::= primaryexpr
148 ///
149 bool AsmParser::ParseExpression(AsmExpr *&Res) {
150   Res = 0;
151   return ParsePrimaryExpr(Res) ||
152          ParseBinOpRHS(1, Res);
153 }
154
155 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
156   AsmExpr *Expr;
157   
158   SMLoc StartLoc = Lexer.getLoc();
159   if (ParseExpression(Expr))
160     return true;
161
162   if (!Expr->EvaluateAsAbsolute(Ctx, Res))
163     return Error(StartLoc, "expected absolute expression");
164
165   return false;
166 }
167
168 bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
169   AsmExpr *Expr;
170   
171   SMLoc StartLoc = Lexer.getLoc();
172   if (ParseExpression(Expr))
173     return true;
174
175   if (!Expr->EvaluateAsRelocatable(Ctx, Res))
176     return Error(StartLoc, "expected relocatable expression");
177
178   return false;
179 }
180
181 bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
182   AsmExpr *Expr;
183   
184   SMLoc StartLoc = Lexer.getLoc();
185   if (ParseParenExpr(Expr))
186     return true;
187
188   if (!Expr->EvaluateAsRelocatable(Ctx, Res))
189     return Error(StartLoc, "expected relocatable expression");
190
191   return false;
192 }
193
194 static unsigned getBinOpPrecedence(AsmToken::TokenKind K, 
195                                    AsmBinaryExpr::Opcode &Kind) {
196   switch (K) {
197   default: return 0;    // not a binop.
198
199     // Lowest Precedence: &&, ||
200   case AsmToken::AmpAmp:
201     Kind = AsmBinaryExpr::LAnd;
202     return 1;
203   case AsmToken::PipePipe:
204     Kind = AsmBinaryExpr::LOr;
205     return 1;
206
207     // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
208   case AsmToken::Plus:
209     Kind = AsmBinaryExpr::Add;
210     return 2;
211   case AsmToken::Minus:
212     Kind = AsmBinaryExpr::Sub;
213     return 2;
214   case AsmToken::EqualEqual:
215     Kind = AsmBinaryExpr::EQ;
216     return 2;
217   case AsmToken::ExclaimEqual:
218   case AsmToken::LessGreater:
219     Kind = AsmBinaryExpr::NE;
220     return 2;
221   case AsmToken::Less:
222     Kind = AsmBinaryExpr::LT;
223     return 2;
224   case AsmToken::LessEqual:
225     Kind = AsmBinaryExpr::LTE;
226     return 2;
227   case AsmToken::Greater:
228     Kind = AsmBinaryExpr::GT;
229     return 2;
230   case AsmToken::GreaterEqual:
231     Kind = AsmBinaryExpr::GTE;
232     return 2;
233
234     // Intermediate Precedence: |, &, ^
235     //
236     // FIXME: gas seems to support '!' as an infix operator?
237   case AsmToken::Pipe:
238     Kind = AsmBinaryExpr::Or;
239     return 3;
240   case AsmToken::Caret:
241     Kind = AsmBinaryExpr::Xor;
242     return 3;
243   case AsmToken::Amp:
244     Kind = AsmBinaryExpr::And;
245     return 3;
246
247     // Highest Precedence: *, /, %, <<, >>
248   case AsmToken::Star:
249     Kind = AsmBinaryExpr::Mul;
250     return 4;
251   case AsmToken::Slash:
252     Kind = AsmBinaryExpr::Div;
253     return 4;
254   case AsmToken::Percent:
255     Kind = AsmBinaryExpr::Mod;
256     return 4;
257   case AsmToken::LessLess:
258     Kind = AsmBinaryExpr::Shl;
259     return 4;
260   case AsmToken::GreaterGreater:
261     Kind = AsmBinaryExpr::Shr;
262     return 4;
263   }
264 }
265
266
267 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
268 /// Res contains the LHS of the expression on input.
269 bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
270   while (1) {
271     AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
272     unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
273     
274     // If the next token is lower precedence than we are allowed to eat, return
275     // successfully with what we ate already.
276     if (TokPrec < Precedence)
277       return false;
278     
279     Lexer.Lex();
280     
281     // Eat the next primary expression.
282     AsmExpr *RHS;
283     if (ParsePrimaryExpr(RHS)) return true;
284     
285     // If BinOp binds less tightly with RHS than the operator after RHS, let
286     // the pending operator take RHS as its LHS.
287     AsmBinaryExpr::Opcode Dummy;
288     unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
289     if (TokPrec < NextTokPrec) {
290       if (ParseBinOpRHS(Precedence+1, RHS)) return true;
291     }
292
293     // Merge LHS and RHS according to operator.
294     Res = new AsmBinaryExpr(Kind, Res, RHS);
295   }
296 }
297
298   
299   
300   
301 /// ParseStatement:
302 ///   ::= EndOfStatement
303 ///   ::= Label* Directive ...Operands... EndOfStatement
304 ///   ::= Label* Identifier OperandList* EndOfStatement
305 bool AsmParser::ParseStatement() {
306   if (Lexer.is(AsmToken::EndOfStatement)) {
307     Lexer.Lex();
308     return false;
309   }
310
311   // Statements always start with an identifier.
312   AsmToken ID = Lexer.getTok();
313   SMLoc IDLoc = ID.getLoc();
314   StringRef IDVal;
315   if (ParseIdentifier(IDVal))
316     return TokError("unexpected token at start of statement");
317
318   // FIXME: Recurse on local labels?
319
320   // See what kind of statement we have.
321   switch (Lexer.getKind()) {
322   case AsmToken::Colon: {
323     // identifier ':'   -> Label.
324     Lexer.Lex();
325
326     // Diagnose attempt to use a variable as a label.
327     //
328     // FIXME: Diagnostics. Note the location of the definition as a label.
329     // FIXME: This doesn't diagnose assignment to a symbol which has been
330     // implicitly marked as external.
331     MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
332     if (Sym->getSection())
333       return Error(IDLoc, "invalid symbol redefinition");
334     if (Ctx.GetSymbolValue(Sym))
335       return Error(IDLoc, "symbol already used as assembler variable");
336     
337     // Since we saw a label, create a symbol and emit it.
338     // FIXME: If the label starts with L it is an assembler temporary label.
339     // Why does the client of this api need to know this?
340     Out.EmitLabel(Sym);
341    
342     return ParseStatement();
343   }
344
345   case AsmToken::Equal:
346     // identifier '=' ... -> assignment statement
347     Lexer.Lex();
348
349     return ParseAssignment(IDVal, false);
350
351   default: // Normal instruction or directive.
352     break;
353   }
354   
355   // Otherwise, we have a normal instruction or directive.  
356   if (IDVal[0] == '.') {
357     // FIXME: This should be driven based on a hash lookup and callback.
358     if (IDVal == ".section")
359       return ParseDirectiveDarwinSection();
360     if (IDVal == ".text")
361       // FIXME: This changes behavior based on the -static flag to the
362       // assembler.
363       return ParseDirectiveSectionSwitch("__TEXT,__text",
364                                          "regular,pure_instructions");
365     if (IDVal == ".const")
366       return ParseDirectiveSectionSwitch("__TEXT,__const");
367     if (IDVal == ".static_const")
368       return ParseDirectiveSectionSwitch("__TEXT,__static_const");
369     if (IDVal == ".cstring")
370       return ParseDirectiveSectionSwitch("__TEXT,__cstring", 
371                                          "cstring_literals");
372     if (IDVal == ".literal4")
373       return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
374     if (IDVal == ".literal8")
375       return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
376     if (IDVal == ".literal16")
377       return ParseDirectiveSectionSwitch("__TEXT,__literal16",
378                                          "16byte_literals");
379     if (IDVal == ".constructor")
380       return ParseDirectiveSectionSwitch("__TEXT,__constructor");
381     if (IDVal == ".destructor")
382       return ParseDirectiveSectionSwitch("__TEXT,__destructor");
383     if (IDVal == ".fvmlib_init0")
384       return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
385     if (IDVal == ".fvmlib_init1")
386       return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
387     if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
388       return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
389                                     "self_modifying_code+pure_instructions,5");
390     // FIXME: .picsymbol_stub on PPC.
391     if (IDVal == ".data")
392       return ParseDirectiveSectionSwitch("__DATA,__data");
393     if (IDVal == ".static_data")
394       return ParseDirectiveSectionSwitch("__DATA,__static_data");
395     if (IDVal == ".non_lazy_symbol_pointer")
396       return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
397                                          "non_lazy_symbol_pointers");
398     if (IDVal == ".lazy_symbol_pointer")
399       return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
400                                          "lazy_symbol_pointers");
401     if (IDVal == ".dyld")
402       return ParseDirectiveSectionSwitch("__DATA,__dyld");
403     if (IDVal == ".mod_init_func")
404       return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
405                                          "mod_init_funcs");
406     if (IDVal == ".mod_term_func")
407       return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
408                                          "mod_term_funcs");
409     if (IDVal == ".const_data")
410       return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
411     
412     
413     // FIXME: Verify attributes on sections.
414     if (IDVal == ".objc_class")
415       return ParseDirectiveSectionSwitch("__OBJC,__class");
416     if (IDVal == ".objc_meta_class")
417       return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
418     if (IDVal == ".objc_cat_cls_meth")
419       return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
420     if (IDVal == ".objc_cat_inst_meth")
421       return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
422     if (IDVal == ".objc_protocol")
423       return ParseDirectiveSectionSwitch("__OBJC,__protocol");
424     if (IDVal == ".objc_string_object")
425       return ParseDirectiveSectionSwitch("__OBJC,__string_object");
426     if (IDVal == ".objc_cls_meth")
427       return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
428     if (IDVal == ".objc_inst_meth")
429       return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
430     if (IDVal == ".objc_cls_refs")
431       return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
432     if (IDVal == ".objc_message_refs")
433       return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
434     if (IDVal == ".objc_symbols")
435       return ParseDirectiveSectionSwitch("__OBJC,__symbols");
436     if (IDVal == ".objc_category")
437       return ParseDirectiveSectionSwitch("__OBJC,__category");
438     if (IDVal == ".objc_class_vars")
439       return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
440     if (IDVal == ".objc_instance_vars")
441       return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
442     if (IDVal == ".objc_module_info")
443       return ParseDirectiveSectionSwitch("__OBJC,__module_info");
444     if (IDVal == ".objc_class_names")
445       return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
446     if (IDVal == ".objc_meth_var_types")
447       return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
448     if (IDVal == ".objc_meth_var_names")
449       return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
450     if (IDVal == ".objc_selector_strs")
451       return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
452     
453     // Assembler features
454     if (IDVal == ".set")
455       return ParseDirectiveSet();
456
457     // Data directives
458
459     if (IDVal == ".ascii")
460       return ParseDirectiveAscii(false);
461     if (IDVal == ".asciz")
462       return ParseDirectiveAscii(true);
463
464     // FIXME: Target hooks for size? Also for "word", "hword".
465     if (IDVal == ".byte")
466       return ParseDirectiveValue(1);
467     if (IDVal == ".short")
468       return ParseDirectiveValue(2);
469     if (IDVal == ".long")
470       return ParseDirectiveValue(4);
471     if (IDVal == ".quad")
472       return ParseDirectiveValue(8);
473
474     // FIXME: Target hooks for IsPow2.
475     if (IDVal == ".align")
476       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
477     if (IDVal == ".align32")
478       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
479     if (IDVal == ".balign")
480       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
481     if (IDVal == ".balignw")
482       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
483     if (IDVal == ".balignl")
484       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
485     if (IDVal == ".p2align")
486       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
487     if (IDVal == ".p2alignw")
488       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
489     if (IDVal == ".p2alignl")
490       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
491
492     if (IDVal == ".org")
493       return ParseDirectiveOrg();
494
495     if (IDVal == ".fill")
496       return ParseDirectiveFill();
497     if (IDVal == ".space")
498       return ParseDirectiveSpace();
499
500     // Symbol attribute directives
501     if (IDVal == ".globl" || IDVal == ".global")
502       return ParseDirectiveSymbolAttribute(MCStreamer::Global);
503     if (IDVal == ".hidden")
504       return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
505     if (IDVal == ".indirect_symbol")
506       return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
507     if (IDVal == ".internal")
508       return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
509     if (IDVal == ".lazy_reference")
510       return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
511     if (IDVal == ".no_dead_strip")
512       return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
513     if (IDVal == ".private_extern")
514       return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
515     if (IDVal == ".protected")
516       return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
517     if (IDVal == ".reference")
518       return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
519     if (IDVal == ".weak")
520       return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
521     if (IDVal == ".weak_definition")
522       return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
523     if (IDVal == ".weak_reference")
524       return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
525
526     if (IDVal == ".comm")
527       return ParseDirectiveComm(/*IsLocal=*/false);
528     if (IDVal == ".lcomm")
529       return ParseDirectiveComm(/*IsLocal=*/true);
530     if (IDVal == ".zerofill")
531       return ParseDirectiveDarwinZerofill();
532     if (IDVal == ".desc")
533       return ParseDirectiveDarwinSymbolDesc();
534     if (IDVal == ".lsym")
535       return ParseDirectiveDarwinLsym();
536
537     if (IDVal == ".subsections_via_symbols")
538       return ParseDirectiveDarwinSubsectionsViaSymbols();
539     if (IDVal == ".abort")
540       return ParseDirectiveAbort();
541     if (IDVal == ".include")
542       return ParseDirectiveInclude();
543     if (IDVal == ".dump")
544       return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
545     if (IDVal == ".load")
546       return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
547
548     Warning(IDLoc, "ignoring directive for now");
549     EatToEndOfStatement();
550     return false;
551   }
552
553   MCInst Inst;
554   if (getTargetParser().ParseInstruction(IDVal, Inst))
555     return true;
556   
557   if (Lexer.isNot(AsmToken::EndOfStatement))
558     return TokError("unexpected token in argument list");
559
560   // Eat the end of statement marker.
561   Lexer.Lex();
562   
563   // Instruction is good, process it.
564   Out.EmitInstruction(Inst);
565   
566   // Skip to end of line for now.
567   return false;
568 }
569
570 bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
571   // FIXME: Use better location, we should use proper tokens.
572   SMLoc EqualLoc = Lexer.getLoc();
573
574   MCValue Value;
575   if (ParseRelocatableExpression(Value))
576     return true;
577   
578   if (Lexer.isNot(AsmToken::EndOfStatement))
579     return TokError("unexpected token in assignment");
580
581   // Eat the end of statement marker.
582   Lexer.Lex();
583
584   // Diagnose assignment to a label.
585   //
586   // FIXME: Diagnostics. Note the location of the definition as a label.
587   // FIXME: This doesn't diagnose assignment to a symbol which has been
588   // implicitly marked as external.
589   // FIXME: Handle '.'.
590   // FIXME: Diagnose assignment to protected identifier (e.g., register name).
591   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
592   if (Sym->getSection())
593     return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
594   if (Sym->isExternal())
595     return Error(EqualLoc, "invalid assignment to external symbol");
596
597   // Do the assignment.
598   Out.EmitAssignment(Sym, Value, IsDotSet);
599
600   return false;
601 }
602
603 /// ParseIdentifier:
604 ///   ::= identifier
605 ///   ::= string
606 bool AsmParser::ParseIdentifier(StringRef &Res) {
607   if (Lexer.isNot(AsmToken::Identifier) &&
608       Lexer.isNot(AsmToken::String))
609     return true;
610
611   Res = Lexer.getTok().getIdentifier();
612
613   Lexer.Lex(); // Consume the identifier token.
614
615   return false;
616 }
617
618 /// ParseDirectiveSet:
619 ///   ::= .set identifier ',' expression
620 bool AsmParser::ParseDirectiveSet() {
621   StringRef Name;
622
623   if (ParseIdentifier(Name))
624     return TokError("expected identifier after '.set' directive");
625   
626   if (Lexer.isNot(AsmToken::Comma))
627     return TokError("unexpected token in '.set'");
628   Lexer.Lex();
629
630   return ParseAssignment(Name, true);
631 }
632
633 /// ParseDirectiveSection:
634 ///   ::= .section identifier (',' identifier)*
635 /// FIXME: This should actually parse out the segment, section, attributes and
636 /// sizeof_stub fields.
637 bool AsmParser::ParseDirectiveDarwinSection() {
638   StringRef SectionName;
639
640   if (ParseIdentifier(SectionName))
641     return TokError("expected identifier after '.section' directive");
642   
643   std::string Section = SectionName;
644
645   // FIXME: This doesn't work, we lose quoting on things
646
647   // Accept a comma separated list of modifiers.
648   while (Lexer.is(AsmToken::Comma)) {
649     Lexer.Lex(); // Consume the comma.
650
651     StringRef ModifierName;    
652     if (ParseIdentifier(ModifierName))
653       return TokError("expected identifier in '.section' directive");
654     Section += ',';
655     Section += ModifierName;
656   }
657   
658   if (Lexer.isNot(AsmToken::EndOfStatement))
659     return TokError("unexpected token in '.section' directive");
660   Lexer.Lex();
661
662   // FIXME: Arch specific.
663   MCSection *S = Ctx.GetSection(Section);
664   if (S == 0)
665     S = MCSection::Create(Section, false, SectionKind(), Ctx);
666   
667   Out.SwitchSection(S);
668   return false;
669 }
670
671 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
672                                             const char *Directives) {
673   if (Lexer.isNot(AsmToken::EndOfStatement))
674     return TokError("unexpected token in section switching directive");
675   Lexer.Lex();
676   
677   std::string SectionStr = Section;
678   if (Directives && Directives[0]) {
679     SectionStr += ","; 
680     SectionStr += Directives;
681   }
682   
683   // FIXME: Arch specific.
684   MCSection *S = Ctx.GetSection(Section);
685   if (S == 0)
686     S = MCSection::Create(Section, false, SectionKind(), Ctx);
687   
688   Out.SwitchSection(S);
689   return false;
690 }
691
692 /// ParseDirectiveAscii:
693 ///   ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
694 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
695   if (Lexer.isNot(AsmToken::EndOfStatement)) {
696     for (;;) {
697       if (Lexer.isNot(AsmToken::String))
698         return TokError("expected string in '.ascii' or '.asciz' directive");
699       
700       // FIXME: This shouldn't use a const char* + strlen, the string could have
701       // embedded nulls.
702       // FIXME: Should have accessor for getting string contents.
703       StringRef Str = Lexer.getTok().getString();
704       Out.EmitBytes(Str.substr(1, Str.size() - 2));
705       if (ZeroTerminated)
706         Out.EmitBytes(StringRef("\0", 1));
707       
708       Lexer.Lex();
709       
710       if (Lexer.is(AsmToken::EndOfStatement))
711         break;
712
713       if (Lexer.isNot(AsmToken::Comma))
714         return TokError("unexpected token in '.ascii' or '.asciz' directive");
715       Lexer.Lex();
716     }
717   }
718
719   Lexer.Lex();
720   return false;
721 }
722
723 /// ParseDirectiveValue
724 ///  ::= (.byte | .short | ... ) [ expression (, expression)* ]
725 bool AsmParser::ParseDirectiveValue(unsigned Size) {
726   if (Lexer.isNot(AsmToken::EndOfStatement)) {
727     for (;;) {
728       MCValue Expr;
729       if (ParseRelocatableExpression(Expr))
730         return true;
731
732       Out.EmitValue(Expr, Size);
733
734       if (Lexer.is(AsmToken::EndOfStatement))
735         break;
736       
737       // FIXME: Improve diagnostic.
738       if (Lexer.isNot(AsmToken::Comma))
739         return TokError("unexpected token in directive");
740       Lexer.Lex();
741     }
742   }
743
744   Lexer.Lex();
745   return false;
746 }
747
748 /// ParseDirectiveSpace
749 ///  ::= .space expression [ , expression ]
750 bool AsmParser::ParseDirectiveSpace() {
751   int64_t NumBytes;
752   if (ParseAbsoluteExpression(NumBytes))
753     return true;
754
755   int64_t FillExpr = 0;
756   bool HasFillExpr = false;
757   if (Lexer.isNot(AsmToken::EndOfStatement)) {
758     if (Lexer.isNot(AsmToken::Comma))
759       return TokError("unexpected token in '.space' directive");
760     Lexer.Lex();
761     
762     if (ParseAbsoluteExpression(FillExpr))
763       return true;
764
765     HasFillExpr = true;
766
767     if (Lexer.isNot(AsmToken::EndOfStatement))
768       return TokError("unexpected token in '.space' directive");
769   }
770
771   Lexer.Lex();
772
773   if (NumBytes <= 0)
774     return TokError("invalid number of bytes in '.space' directive");
775
776   // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
777   for (uint64_t i = 0, e = NumBytes; i != e; ++i)
778     Out.EmitValue(MCValue::get(FillExpr), 1);
779
780   return false;
781 }
782
783 /// ParseDirectiveFill
784 ///  ::= .fill expression , expression , expression
785 bool AsmParser::ParseDirectiveFill() {
786   int64_t NumValues;
787   if (ParseAbsoluteExpression(NumValues))
788     return true;
789
790   if (Lexer.isNot(AsmToken::Comma))
791     return TokError("unexpected token in '.fill' directive");
792   Lexer.Lex();
793   
794   int64_t FillSize;
795   if (ParseAbsoluteExpression(FillSize))
796     return true;
797
798   if (Lexer.isNot(AsmToken::Comma))
799     return TokError("unexpected token in '.fill' directive");
800   Lexer.Lex();
801   
802   int64_t FillExpr;
803   if (ParseAbsoluteExpression(FillExpr))
804     return true;
805
806   if (Lexer.isNot(AsmToken::EndOfStatement))
807     return TokError("unexpected token in '.fill' directive");
808   
809   Lexer.Lex();
810
811   if (FillSize != 1 && FillSize != 2 && FillSize != 4)
812     return TokError("invalid '.fill' size, expected 1, 2, or 4");
813
814   for (uint64_t i = 0, e = NumValues; i != e; ++i)
815     Out.EmitValue(MCValue::get(FillExpr), FillSize);
816
817   return false;
818 }
819
820 /// ParseDirectiveOrg
821 ///  ::= .org expression [ , expression ]
822 bool AsmParser::ParseDirectiveOrg() {
823   MCValue Offset;
824   if (ParseRelocatableExpression(Offset))
825     return true;
826
827   // Parse optional fill expression.
828   int64_t FillExpr = 0;
829   if (Lexer.isNot(AsmToken::EndOfStatement)) {
830     if (Lexer.isNot(AsmToken::Comma))
831       return TokError("unexpected token in '.org' directive");
832     Lexer.Lex();
833     
834     if (ParseAbsoluteExpression(FillExpr))
835       return true;
836
837     if (Lexer.isNot(AsmToken::EndOfStatement))
838       return TokError("unexpected token in '.org' directive");
839   }
840
841   Lexer.Lex();
842
843   // FIXME: Only limited forms of relocatable expressions are accepted here, it
844   // has to be relative to the current section.
845   Out.EmitValueToOffset(Offset, FillExpr);
846
847   return false;
848 }
849
850 /// ParseDirectiveAlign
851 ///  ::= {.align, ...} expression [ , expression [ , expression ]]
852 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
853   int64_t Alignment;
854   if (ParseAbsoluteExpression(Alignment))
855     return true;
856
857   SMLoc MaxBytesLoc;
858   bool HasFillExpr = false;
859   int64_t FillExpr = 0;
860   int64_t MaxBytesToFill = 0;
861   if (Lexer.isNot(AsmToken::EndOfStatement)) {
862     if (Lexer.isNot(AsmToken::Comma))
863       return TokError("unexpected token in directive");
864     Lexer.Lex();
865
866     // The fill expression can be omitted while specifying a maximum number of
867     // alignment bytes, e.g:
868     //  .align 3,,4
869     if (Lexer.isNot(AsmToken::Comma)) {
870       HasFillExpr = true;
871       if (ParseAbsoluteExpression(FillExpr))
872         return true;
873     }
874
875     if (Lexer.isNot(AsmToken::EndOfStatement)) {
876       if (Lexer.isNot(AsmToken::Comma))
877         return TokError("unexpected token in directive");
878       Lexer.Lex();
879
880       MaxBytesLoc = Lexer.getLoc();
881       if (ParseAbsoluteExpression(MaxBytesToFill))
882         return true;
883       
884       if (Lexer.isNot(AsmToken::EndOfStatement))
885         return TokError("unexpected token in directive");
886     }
887   }
888
889   Lexer.Lex();
890
891   if (!HasFillExpr) {
892     // FIXME: Sometimes fill with nop.
893     FillExpr = 0;
894   }
895
896   // Compute alignment in bytes.
897   if (IsPow2) {
898     // FIXME: Diagnose overflow.
899     Alignment = 1LL << Alignment;
900   }
901
902   // Diagnose non-sensical max bytes to fill.
903   if (MaxBytesLoc.isValid()) {
904     if (MaxBytesToFill < 1) {
905       Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
906               "many bytes, ignoring");
907       return false;
908     }
909
910     if (MaxBytesToFill >= Alignment) {
911       Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
912               "has no effect");
913       MaxBytesToFill = 0;
914     }
915   }
916
917   // FIXME: Target specific behavior about how the "extra" bytes are filled.
918   Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
919
920   return false;
921 }
922
923 /// ParseDirectiveSymbolAttribute
924 ///  ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
925 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
926   if (Lexer.isNot(AsmToken::EndOfStatement)) {
927     for (;;) {
928       StringRef Name;
929
930       if (ParseIdentifier(Name))
931         return TokError("expected identifier in directive");
932       
933       MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
934
935       // If this is use of an undefined symbol then mark it external.
936       if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
937         Sym->setExternal(true);
938
939       Out.EmitSymbolAttribute(Sym, Attr);
940
941       if (Lexer.is(AsmToken::EndOfStatement))
942         break;
943
944       if (Lexer.isNot(AsmToken::Comma))
945         return TokError("unexpected token in directive");
946       Lexer.Lex();
947     }
948   }
949
950   Lexer.Lex();
951   return false;  
952 }
953
954 /// ParseDirectiveDarwinSymbolDesc
955 ///  ::= .desc identifier , expression
956 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
957   StringRef Name;
958   if (ParseIdentifier(Name))
959     return TokError("expected identifier in directive");
960   
961   // Handle the identifier as the key symbol.
962   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
963
964   if (Lexer.isNot(AsmToken::Comma))
965     return TokError("unexpected token in '.desc' directive");
966   Lexer.Lex();
967
968   SMLoc DescLoc = Lexer.getLoc();
969   int64_t DescValue;
970   if (ParseAbsoluteExpression(DescValue))
971     return true;
972
973   if (Lexer.isNot(AsmToken::EndOfStatement))
974     return TokError("unexpected token in '.desc' directive");
975   
976   Lexer.Lex();
977
978   // Set the n_desc field of this Symbol to this DescValue
979   Out.EmitSymbolDesc(Sym, DescValue);
980
981   return false;
982 }
983
984 /// ParseDirectiveComm
985 ///  ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
986 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
987   SMLoc IDLoc = Lexer.getLoc();
988   StringRef Name;
989   if (ParseIdentifier(Name))
990     return TokError("expected identifier in directive");
991   
992   // Handle the identifier as the key symbol.
993   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
994
995   if (Lexer.isNot(AsmToken::Comma))
996     return TokError("unexpected token in directive");
997   Lexer.Lex();
998
999   int64_t Size;
1000   SMLoc SizeLoc = Lexer.getLoc();
1001   if (ParseAbsoluteExpression(Size))
1002     return true;
1003
1004   int64_t Pow2Alignment = 0;
1005   SMLoc Pow2AlignmentLoc;
1006   if (Lexer.is(AsmToken::Comma)) {
1007     Lexer.Lex();
1008     Pow2AlignmentLoc = Lexer.getLoc();
1009     if (ParseAbsoluteExpression(Pow2Alignment))
1010       return true;
1011   }
1012   
1013   if (Lexer.isNot(AsmToken::EndOfStatement))
1014     return TokError("unexpected token in '.comm' or '.lcomm' directive");
1015   
1016   Lexer.Lex();
1017
1018   // NOTE: a size of zero for a .comm should create a undefined symbol
1019   // but a size of .lcomm creates a bss symbol of size zero.
1020   if (Size < 0)
1021     return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1022                  "be less than zero");
1023
1024   // NOTE: The alignment in the directive is a power of 2 value, the assember
1025   // may internally end up wanting an alignment in bytes.
1026   // FIXME: Diagnose overflow.
1027   if (Pow2Alignment < 0)
1028     return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1029                  "alignment, can't be less than zero");
1030
1031   // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1032   if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1033     return Error(IDLoc, "invalid symbol redefinition");
1034
1035   // Create the Symbol as a common or local common with Size and Pow2Alignment
1036   Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
1037
1038   return false;
1039 }
1040
1041 /// ParseDirectiveDarwinZerofill
1042 ///  ::= .zerofill segname , sectname [, identifier , size_expression [
1043 ///      , align_expression ]]
1044 bool AsmParser::ParseDirectiveDarwinZerofill() {
1045   // FIXME: Handle quoted names here.
1046
1047   if (Lexer.isNot(AsmToken::Identifier))
1048     return TokError("expected segment name after '.zerofill' directive");
1049   std::string Section = Lexer.getTok().getString();
1050   Lexer.Lex();
1051
1052   if (Lexer.isNot(AsmToken::Comma))
1053     return TokError("unexpected token in directive");
1054   Section += ',';
1055   Lexer.Lex();
1056  
1057   if (Lexer.isNot(AsmToken::Identifier))
1058     return TokError("expected section name after comma in '.zerofill' "
1059                     "directive");
1060   Section += Lexer.getTok().getString().str();
1061   Lexer.Lex();
1062
1063   // FIXME: we will need to tell GetSection() that this is to be created with or
1064   // must have the Mach-O section type of S_ZEROFILL.  Something like the code
1065   // below could be done but for now it is not as EmitZerofill() does not know
1066   // how to deal with a section type in the section name like
1067   // ParseDirectiveDarwinSection() allows.
1068   // Section += ',';
1069   // Section += "zerofill";
1070
1071   // If this is the end of the line all that was wanted was to create the
1072   // the section but with no symbol.
1073   if (Lexer.is(AsmToken::EndOfStatement)) {
1074     // FIXME: Arch specific.
1075     MCSection *S = Ctx.GetSection(Section);
1076     if (S == 0)
1077       S = MCSection::Create(Section, false, SectionKind(), Ctx);
1078     
1079     // Create the zerofill section but no symbol
1080     Out.EmitZerofill(S);
1081     return false;
1082   }
1083
1084   if (Lexer.isNot(AsmToken::Comma))
1085     return TokError("unexpected token in directive");
1086   Lexer.Lex();
1087
1088   if (Lexer.isNot(AsmToken::Identifier))
1089     return TokError("expected identifier in directive");
1090   
1091   // handle the identifier as the key symbol.
1092   SMLoc IDLoc = Lexer.getLoc();
1093   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
1094   Lexer.Lex();
1095
1096   if (Lexer.isNot(AsmToken::Comma))
1097     return TokError("unexpected token in directive");
1098   Lexer.Lex();
1099
1100   int64_t Size;
1101   SMLoc SizeLoc = Lexer.getLoc();
1102   if (ParseAbsoluteExpression(Size))
1103     return true;
1104
1105   int64_t Pow2Alignment = 0;
1106   SMLoc Pow2AlignmentLoc;
1107   if (Lexer.is(AsmToken::Comma)) {
1108     Lexer.Lex();
1109     Pow2AlignmentLoc = Lexer.getLoc();
1110     if (ParseAbsoluteExpression(Pow2Alignment))
1111       return true;
1112   }
1113   
1114   if (Lexer.isNot(AsmToken::EndOfStatement))
1115     return TokError("unexpected token in '.zerofill' directive");
1116   
1117   Lexer.Lex();
1118
1119   if (Size < 0)
1120     return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1121                  "than zero");
1122
1123   // NOTE: The alignment in the directive is a power of 2 value, the assember
1124   // may internally end up wanting an alignment in bytes.
1125   // FIXME: Diagnose overflow.
1126   if (Pow2Alignment < 0)
1127     return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1128                  "can't be less than zero");
1129
1130   // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1131   if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1132     return Error(IDLoc, "invalid symbol redefinition");
1133
1134   // FIXME: Arch specific.
1135   MCSection *S = Ctx.GetSection(Section);
1136   if (S == 0)
1137     S = MCSection::Create(Section, false, SectionKind(), Ctx);
1138   
1139   // Create the zerofill Symbol with Size and Pow2Alignment
1140   Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
1141
1142   return false;
1143 }
1144
1145 /// ParseDirectiveDarwinSubsectionsViaSymbols
1146 ///  ::= .subsections_via_symbols
1147 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1148   if (Lexer.isNot(AsmToken::EndOfStatement))
1149     return TokError("unexpected token in '.subsections_via_symbols' directive");
1150   
1151   Lexer.Lex();
1152
1153   Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1154
1155   return false;
1156 }
1157
1158 /// ParseDirectiveAbort
1159 ///  ::= .abort [ "abort_string" ]
1160 bool AsmParser::ParseDirectiveAbort() {
1161   // FIXME: Use loc from directive.
1162   SMLoc Loc = Lexer.getLoc();
1163
1164   StringRef Str = "";
1165   if (Lexer.isNot(AsmToken::EndOfStatement)) {
1166     if (Lexer.isNot(AsmToken::String))
1167       return TokError("expected string in '.abort' directive");
1168     
1169     Str = Lexer.getTok().getString();
1170
1171     Lexer.Lex();
1172   }
1173
1174   if (Lexer.isNot(AsmToken::EndOfStatement))
1175     return TokError("unexpected token in '.abort' directive");
1176   
1177   Lexer.Lex();
1178
1179   // FIXME: Handle here.
1180   if (Str.empty())
1181     Error(Loc, ".abort detected. Assembly stopping.");
1182   else
1183     Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1184
1185   return false;
1186 }
1187
1188 /// ParseDirectiveLsym
1189 ///  ::= .lsym identifier , expression
1190 bool AsmParser::ParseDirectiveDarwinLsym() {
1191   StringRef Name;
1192   if (ParseIdentifier(Name))
1193     return TokError("expected identifier in directive");
1194   
1195   // Handle the identifier as the key symbol.
1196   MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
1197
1198   if (Lexer.isNot(AsmToken::Comma))
1199     return TokError("unexpected token in '.lsym' directive");
1200   Lexer.Lex();
1201
1202   MCValue Expr;
1203   if (ParseRelocatableExpression(Expr))
1204     return true;
1205
1206   if (Lexer.isNot(AsmToken::EndOfStatement))
1207     return TokError("unexpected token in '.lsym' directive");
1208   
1209   Lexer.Lex();
1210
1211   // Create the Sym with the value of the Expr
1212   Out.EmitLocalSymbol(Sym, Expr);
1213
1214   return false;
1215 }
1216
1217 /// ParseDirectiveInclude
1218 ///  ::= .include "filename"
1219 bool AsmParser::ParseDirectiveInclude() {
1220   if (Lexer.isNot(AsmToken::String))
1221     return TokError("expected string in '.include' directive");
1222   
1223   std::string Filename = Lexer.getTok().getString();
1224   SMLoc IncludeLoc = Lexer.getLoc();
1225   Lexer.Lex();
1226
1227   if (Lexer.isNot(AsmToken::EndOfStatement))
1228     return TokError("unexpected token in '.include' directive");
1229   
1230   // Strip the quotes.
1231   Filename = Filename.substr(1, Filename.size()-2);
1232   
1233   // Attempt to switch the lexer to the included file before consuming the end
1234   // of statement to avoid losing it when we switch.
1235   if (Lexer.EnterIncludeFile(Filename)) {
1236     Lexer.PrintMessage(IncludeLoc,
1237                        "Could not find include file '" + Filename + "'",
1238                        "error");
1239     return true;
1240   }
1241
1242   return false;
1243 }
1244
1245 /// ParseDirectiveDarwinDumpOrLoad
1246 ///  ::= ( .dump | .load ) "filename"
1247 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1248   if (Lexer.isNot(AsmToken::String))
1249     return TokError("expected string in '.dump' or '.load' directive");
1250   
1251   Lexer.Lex();
1252
1253   if (Lexer.isNot(AsmToken::EndOfStatement))
1254     return TokError("unexpected token in '.dump' or '.load' directive");
1255   
1256   Lexer.Lex();
1257
1258   // FIXME: If/when .dump and .load are implemented they will be done in the
1259   // the assembly parser and not have any need for an MCStreamer API.
1260   if (IsDump)
1261     Warning(IDLoc, "ignoring directive .dump for now");
1262   else
1263     Warning(IDLoc, "ignoring directive .load for now");
1264
1265   return false;
1266 }