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