AsmParser.cpp revision 0143ac136847977bea1ba49f4f343feeabfc6a13
1//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/APFloat.h"
15#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26#include "llvm/MC/MCSectionMachO.h"
27#include "llvm/MC/MCStreamer.h"
28#include "llvm/MC/MCSymbol.h"
29#include "llvm/MC/MCDwarf.h"
30#include "llvm/Support/MemoryBuffer.h"
31#include "llvm/Support/SourceMgr.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/Target/TargetAsmInfo.h"
34#include "llvm/Target/TargetAsmParser.h"
35#include <cctype>
36#include <vector>
37using namespace llvm;
38
39namespace {
40
41/// \brief Helper class for tracking macro definitions.
42struct Macro {
43  StringRef Name;
44  StringRef Body;
45
46public:
47  Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
48};
49
50/// \brief Helper class for storing information about an active macro
51/// instantiation.
52struct MacroInstantiation {
53  /// The macro being instantiated.
54  const Macro *TheMacro;
55
56  /// The macro instantiation with substitutions.
57  MemoryBuffer *Instantiation;
58
59  /// The location of the instantiation.
60  SMLoc InstantiationLoc;
61
62  /// The location where parsing should resume upon instantiation completion.
63  SMLoc ExitLoc;
64
65public:
66  MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
67                     const std::vector<std::vector<AsmToken> > &A);
68};
69
70/// \brief The concrete assembly parser instance.
71class AsmParser : public MCAsmParser {
72  friend class GenericAsmParser;
73
74  AsmParser(const AsmParser &);   // DO NOT IMPLEMENT
75  void operator=(const AsmParser &);  // DO NOT IMPLEMENT
76private:
77  AsmLexer Lexer;
78  MCContext &Ctx;
79  MCStreamer &Out;
80  SourceMgr &SrcMgr;
81  MCAsmParserExtension *GenericParser;
82  MCAsmParserExtension *PlatformParser;
83
84  /// This is the current buffer index we're lexing from as managed by the
85  /// SourceMgr object.
86  int CurBuffer;
87
88  AsmCond TheCondState;
89  std::vector<AsmCond> TheCondStack;
90
91  /// DirectiveMap - This is a table handlers for directives.  Each handler is
92  /// invoked after the directive identifier is read and is responsible for
93  /// parsing and validating the rest of the directive.  The handler is passed
94  /// in the directive name and the location of the directive keyword.
95  StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
96
97  /// MacroMap - Map of currently defined macros.
98  StringMap<Macro*> MacroMap;
99
100  /// ActiveMacros - Stack of active macro instantiations.
101  std::vector<MacroInstantiation*> ActiveMacros;
102
103  /// Boolean tracking whether macro substitution is enabled.
104  unsigned MacrosEnabled : 1;
105
106  /// Flag tracking whether any errors have been encountered.
107  unsigned HadError : 1;
108
109public:
110  AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
111            const MCAsmInfo &MAI);
112  ~AsmParser();
113
114  virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
115
116  void AddDirectiveHandler(MCAsmParserExtension *Object,
117                           StringRef Directive,
118                           DirectiveHandler Handler) {
119    DirectiveMap[Directive] = std::make_pair(Object, Handler);
120  }
121
122public:
123  /// @name MCAsmParser Interface
124  /// {
125
126  virtual SourceMgr &getSourceManager() { return SrcMgr; }
127  virtual MCAsmLexer &getLexer() { return Lexer; }
128  virtual MCContext &getContext() { return Ctx; }
129  virtual MCStreamer &getStreamer() { return Out; }
130
131  virtual void Warning(SMLoc L, const Twine &Meg);
132  virtual bool Error(SMLoc L, const Twine &Msg);
133
134  const AsmToken &Lex();
135
136  bool ParseExpression(const MCExpr *&Res);
137  virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
138  virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
139  virtual bool ParseAbsoluteExpression(int64_t &Res);
140
141  /// }
142
143private:
144  void CheckForValidSection();
145
146  bool ParseStatement();
147
148  bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
149  void HandleMacroExit();
150
151  void PrintMacroInstantiations();
152  void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
153    SrcMgr.PrintMessage(Loc, Msg, Type);
154  }
155
156  /// EnterIncludeFile - Enter the specified file. This returns true on failure.
157  bool EnterIncludeFile(const std::string &Filename);
158
159  /// \brief Reset the current lexer position to that given by \arg Loc. The
160  /// current token is not set; clients should ensure Lex() is called
161  /// subsequently.
162  void JumpToLoc(SMLoc Loc);
163
164  void EatToEndOfStatement();
165
166  /// \brief Parse up to the end of statement and a return the contents from the
167  /// current token until the end of the statement; the current token on exit
168  /// will be either the EndOfStatement or EOF.
169  StringRef ParseStringToEndOfStatement();
170
171  bool ParseAssignment(StringRef Name, bool allow_redef);
172
173  bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
174  bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
175  bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
176  bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
177
178  /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
179  /// and set \arg Res to the identifier contents.
180  bool ParseIdentifier(StringRef &Res);
181
182  // Directive Parsing.
183
184 // ".ascii", ".asciiz", ".string"
185  bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
186  bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
187  bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
188  bool ParseDirectiveFill(); // ".fill"
189  bool ParseDirectiveSpace(); // ".space"
190  bool ParseDirectiveZero(); // ".zero"
191  bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
192  bool ParseDirectiveOrg(); // ".org"
193  // ".align{,32}", ".p2align{,w,l}"
194  bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
195
196  /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
197  /// accepts a single symbol (which should be a label or an external).
198  bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
199
200  bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
201
202  bool ParseDirectiveAbort(); // ".abort"
203  bool ParseDirectiveInclude(); // ".include"
204
205  bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
206  // ".ifdef" or ".ifndef", depending on expect_defined
207  bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
208  bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
209  bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
210  bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
211
212  /// ParseEscapedString - Parse the current token as a string which may include
213  /// escaped characters and return the string contents.
214  bool ParseEscapedString(std::string &Data);
215
216  const MCExpr *ApplyModifierToExpr(const MCExpr *E,
217                                    MCSymbolRefExpr::VariantKind Variant);
218};
219
220/// \brief Generic implementations of directive handling, etc. which is shared
221/// (or the default, at least) for all assembler parser.
222class GenericAsmParser : public MCAsmParserExtension {
223  template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
224  void AddDirectiveHandler(StringRef Directive) {
225    getParser().AddDirectiveHandler(this, Directive,
226                                    HandleDirective<GenericAsmParser, Handler>);
227  }
228public:
229  GenericAsmParser() {}
230
231  AsmParser &getParser() {
232    return (AsmParser&) this->MCAsmParserExtension::getParser();
233  }
234
235  virtual void Initialize(MCAsmParser &Parser) {
236    // Call the base implementation.
237    this->MCAsmParserExtension::Initialize(Parser);
238
239    // Debugging directives.
240    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
241    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
242    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
243    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
244
245    // CFI directives.
246    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
247                                                              ".cfi_startproc");
248    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
249                                                                ".cfi_endproc");
250    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
251                                                         ".cfi_def_cfa");
252    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
253                                                         ".cfi_def_cfa_offset");
254    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
255                                                       ".cfi_def_cfa_register");
256    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
257                                                                 ".cfi_offset");
258    AddDirectiveHandler<
259     &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
260    AddDirectiveHandler<
261            &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
262    AddDirectiveHandler<
263      &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
264    AddDirectiveHandler<
265      &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
266
267    // Macro directives.
268    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
269      ".macros_on");
270    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
271      ".macros_off");
272    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
273    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
274    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
275
276    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
277    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
278  }
279
280  bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
281
282  bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
283  bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
284  bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
285  bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
286  bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
287  bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
288  bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
289  bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
290  bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
291  bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
292  bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
293  bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
294  bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
295
296  bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
297  bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
298  bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
299
300  bool ParseDirectiveLEB128(StringRef, SMLoc);
301};
302
303}
304
305namespace llvm {
306
307extern MCAsmParserExtension *createDarwinAsmParser();
308extern MCAsmParserExtension *createELFAsmParser();
309extern MCAsmParserExtension *createCOFFAsmParser();
310
311}
312
313enum { DEFAULT_ADDRSPACE = 0 };
314
315AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
316                     MCStreamer &_Out, const MCAsmInfo &_MAI)
317  : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
318    GenericParser(new GenericAsmParser), PlatformParser(0),
319    CurBuffer(0), MacrosEnabled(true) {
320  Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
321
322  // Initialize the generic parser.
323  GenericParser->Initialize(*this);
324
325  // Initialize the platform / file format parser.
326  //
327  // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
328  // created.
329  if (_MAI.hasMicrosoftFastStdCallMangling()) {
330    PlatformParser = createCOFFAsmParser();
331    PlatformParser->Initialize(*this);
332  } else if (_MAI.hasSubsectionsViaSymbols()) {
333    PlatformParser = createDarwinAsmParser();
334    PlatformParser->Initialize(*this);
335  } else {
336    PlatformParser = createELFAsmParser();
337    PlatformParser->Initialize(*this);
338  }
339}
340
341AsmParser::~AsmParser() {
342  assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
343
344  // Destroy any macros.
345  for (StringMap<Macro*>::iterator it = MacroMap.begin(),
346         ie = MacroMap.end(); it != ie; ++it)
347    delete it->getValue();
348
349  delete PlatformParser;
350  delete GenericParser;
351}
352
353void AsmParser::PrintMacroInstantiations() {
354  // Print the active macro instantiation stack.
355  for (std::vector<MacroInstantiation*>::const_reverse_iterator
356         it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
357    PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
358                 "note");
359}
360
361void AsmParser::Warning(SMLoc L, const Twine &Msg) {
362  PrintMessage(L, Msg, "warning");
363  PrintMacroInstantiations();
364}
365
366bool AsmParser::Error(SMLoc L, const Twine &Msg) {
367  HadError = true;
368  PrintMessage(L, Msg, "error");
369  PrintMacroInstantiations();
370  return true;
371}
372
373bool AsmParser::EnterIncludeFile(const std::string &Filename) {
374  int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
375  if (NewBuf == -1)
376    return true;
377
378  CurBuffer = NewBuf;
379
380  Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
381
382  return false;
383}
384
385void AsmParser::JumpToLoc(SMLoc Loc) {
386  CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
387  Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
388}
389
390const AsmToken &AsmParser::Lex() {
391  const AsmToken *tok = &Lexer.Lex();
392
393  if (tok->is(AsmToken::Eof)) {
394    // If this is the end of an included file, pop the parent file off the
395    // include stack.
396    SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
397    if (ParentIncludeLoc != SMLoc()) {
398      JumpToLoc(ParentIncludeLoc);
399      tok = &Lexer.Lex();
400    }
401  }
402
403  if (tok->is(AsmToken::Error))
404    Error(Lexer.getErrLoc(), Lexer.getErr());
405
406  return *tok;
407}
408
409bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
410  // Create the initial section, if requested.
411  if (!NoInitialTextSection)
412    Out.InitSections();
413
414  // Prime the lexer.
415  Lex();
416
417  HadError = false;
418  AsmCond StartingCondState = TheCondState;
419
420  // While we have input, parse each statement.
421  while (Lexer.isNot(AsmToken::Eof)) {
422    if (!ParseStatement()) continue;
423
424    // We had an error, validate that one was emitted and recover by skipping to
425    // the next line.
426    assert(HadError && "Parse statement returned an error, but none emitted!");
427    EatToEndOfStatement();
428  }
429
430  if (TheCondState.TheCond != StartingCondState.TheCond ||
431      TheCondState.Ignore != StartingCondState.Ignore)
432    return TokError("unmatched .ifs or .elses");
433
434  // Check to see there are no empty DwarfFile slots.
435  const std::vector<MCDwarfFile *> &MCDwarfFiles =
436    getContext().getMCDwarfFiles();
437  for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
438    if (!MCDwarfFiles[i])
439      TokError("unassigned file number: " + Twine(i) + " for .file directives");
440  }
441
442  // Finalize the output stream if there are no errors and if the client wants
443  // us to.
444  if (!HadError && !NoFinalize)
445    Out.Finish();
446
447  return HadError;
448}
449
450void AsmParser::CheckForValidSection() {
451  if (!getStreamer().getCurrentSection()) {
452    TokError("expected section directive before assembly directive");
453    Out.SwitchSection(Ctx.getMachOSection(
454                        "__TEXT", "__text",
455                        MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
456                        0, SectionKind::getText()));
457  }
458}
459
460/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
461void AsmParser::EatToEndOfStatement() {
462  while (Lexer.isNot(AsmToken::EndOfStatement) &&
463         Lexer.isNot(AsmToken::Eof))
464    Lex();
465
466  // Eat EOL.
467  if (Lexer.is(AsmToken::EndOfStatement))
468    Lex();
469}
470
471StringRef AsmParser::ParseStringToEndOfStatement() {
472  const char *Start = getTok().getLoc().getPointer();
473
474  while (Lexer.isNot(AsmToken::EndOfStatement) &&
475         Lexer.isNot(AsmToken::Eof))
476    Lex();
477
478  const char *End = getTok().getLoc().getPointer();
479  return StringRef(Start, End - Start);
480}
481
482/// ParseParenExpr - Parse a paren expression and return it.
483/// NOTE: This assumes the leading '(' has already been consumed.
484///
485/// parenexpr ::= expr)
486///
487bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
488  if (ParseExpression(Res)) return true;
489  if (Lexer.isNot(AsmToken::RParen))
490    return TokError("expected ')' in parentheses expression");
491  EndLoc = Lexer.getLoc();
492  Lex();
493  return false;
494}
495
496/// ParseBracketExpr - Parse a bracket expression and return it.
497/// NOTE: This assumes the leading '[' has already been consumed.
498///
499/// bracketexpr ::= expr]
500///
501bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
502  if (ParseExpression(Res)) return true;
503  if (Lexer.isNot(AsmToken::RBrac))
504    return TokError("expected ']' in brackets expression");
505  EndLoc = Lexer.getLoc();
506  Lex();
507  return false;
508}
509
510/// ParsePrimaryExpr - Parse a primary expression and return it.
511///  primaryexpr ::= (parenexpr
512///  primaryexpr ::= symbol
513///  primaryexpr ::= number
514///  primaryexpr ::= '.'
515///  primaryexpr ::= ~,+,- primaryexpr
516bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
517  switch (Lexer.getKind()) {
518  default:
519    return TokError("unknown token in expression");
520  case AsmToken::Exclaim:
521    Lex(); // Eat the operator.
522    if (ParsePrimaryExpr(Res, EndLoc))
523      return true;
524    Res = MCUnaryExpr::CreateLNot(Res, getContext());
525    return false;
526  case AsmToken::Dollar:
527  case AsmToken::String:
528  case AsmToken::Identifier: {
529    EndLoc = Lexer.getLoc();
530
531    StringRef Identifier;
532    if (ParseIdentifier(Identifier))
533      return false;
534
535    // This is a symbol reference.
536    std::pair<StringRef, StringRef> Split = Identifier.split('@');
537    MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
538
539    // Lookup the symbol variant if used.
540    MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
541    if (Split.first.size() != Identifier.size()) {
542      Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
543      if (Variant == MCSymbolRefExpr::VK_Invalid) {
544        Variant = MCSymbolRefExpr::VK_None;
545        return TokError("invalid variant '" + Split.second + "'");
546      }
547    }
548
549    // If this is an absolute variable reference, substitute it now to preserve
550    // semantics in the face of reassignment.
551    if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
552      if (Variant)
553        return Error(EndLoc, "unexpected modifier on variable reference");
554
555      Res = Sym->getVariableValue();
556      return false;
557    }
558
559    // Otherwise create a symbol ref.
560    Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
561    return false;
562  }
563  case AsmToken::Integer: {
564    SMLoc Loc = getTok().getLoc();
565    int64_t IntVal = getTok().getIntVal();
566    Res = MCConstantExpr::Create(IntVal, getContext());
567    EndLoc = Lexer.getLoc();
568    Lex(); // Eat token.
569    // Look for 'b' or 'f' following an Integer as a directional label
570    if (Lexer.getKind() == AsmToken::Identifier) {
571      StringRef IDVal = getTok().getString();
572      if (IDVal == "f" || IDVal == "b"){
573        MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
574                                                      IDVal == "f" ? 1 : 0);
575        Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
576                                      getContext());
577        if(IDVal == "b" && Sym->isUndefined())
578          return Error(Loc, "invalid reference to undefined symbol");
579        EndLoc = Lexer.getLoc();
580        Lex(); // Eat identifier.
581      }
582    }
583    return false;
584  }
585  case AsmToken::Real: {
586    APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
587    uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
588    Res = MCConstantExpr::Create(IntVal, getContext());
589    Lex(); // Eat token.
590    return false;
591  }
592  case AsmToken::Dot: {
593    // This is a '.' reference, which references the current PC.  Emit a
594    // temporary label to the streamer and refer to it.
595    MCSymbol *Sym = Ctx.CreateTempSymbol();
596    Out.EmitLabel(Sym);
597    Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
598    EndLoc = Lexer.getLoc();
599    Lex(); // Eat identifier.
600    return false;
601  }
602  case AsmToken::LParen:
603    Lex(); // Eat the '('.
604    return ParseParenExpr(Res, EndLoc);
605  case AsmToken::LBrac:
606    if (!PlatformParser->HasBracketExpressions())
607      return TokError("brackets expression not supported on this target");
608    Lex(); // Eat the '['.
609    return ParseBracketExpr(Res, EndLoc);
610  case AsmToken::Minus:
611    Lex(); // Eat the operator.
612    if (ParsePrimaryExpr(Res, EndLoc))
613      return true;
614    Res = MCUnaryExpr::CreateMinus(Res, getContext());
615    return false;
616  case AsmToken::Plus:
617    Lex(); // Eat the operator.
618    if (ParsePrimaryExpr(Res, EndLoc))
619      return true;
620    Res = MCUnaryExpr::CreatePlus(Res, getContext());
621    return false;
622  case AsmToken::Tilde:
623    Lex(); // Eat the operator.
624    if (ParsePrimaryExpr(Res, EndLoc))
625      return true;
626    Res = MCUnaryExpr::CreateNot(Res, getContext());
627    return false;
628  }
629}
630
631bool AsmParser::ParseExpression(const MCExpr *&Res) {
632  SMLoc EndLoc;
633  return ParseExpression(Res, EndLoc);
634}
635
636const MCExpr *
637AsmParser::ApplyModifierToExpr(const MCExpr *E,
638                               MCSymbolRefExpr::VariantKind Variant) {
639  // Recurse over the given expression, rebuilding it to apply the given variant
640  // if there is exactly one symbol.
641  switch (E->getKind()) {
642  case MCExpr::Target:
643  case MCExpr::Constant:
644    return 0;
645
646  case MCExpr::SymbolRef: {
647    const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
648
649    if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
650      TokError("invalid variant on expression '" +
651               getTok().getIdentifier() + "' (already modified)");
652      return E;
653    }
654
655    return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
656  }
657
658  case MCExpr::Unary: {
659    const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
660    const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
661    if (!Sub)
662      return 0;
663    return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
664  }
665
666  case MCExpr::Binary: {
667    const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
668    const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
669    const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
670
671    if (!LHS && !RHS)
672      return 0;
673
674    if (!LHS) LHS = BE->getLHS();
675    if (!RHS) RHS = BE->getRHS();
676
677    return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
678  }
679  }
680
681  assert(0 && "Invalid expression kind!");
682  return 0;
683}
684
685/// ParseExpression - Parse an expression and return it.
686///
687///  expr ::= expr +,- expr          -> lowest.
688///  expr ::= expr |,^,&,! expr      -> middle.
689///  expr ::= expr *,/,%,<<,>> expr  -> highest.
690///  expr ::= primaryexpr
691///
692bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
693  // Parse the expression.
694  Res = 0;
695  if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
696    return true;
697
698  // As a special case, we support 'a op b @ modifier' by rewriting the
699  // expression to include the modifier. This is inefficient, but in general we
700  // expect users to use 'a@modifier op b'.
701  if (Lexer.getKind() == AsmToken::At) {
702    Lex();
703
704    if (Lexer.isNot(AsmToken::Identifier))
705      return TokError("unexpected symbol modifier following '@'");
706
707    MCSymbolRefExpr::VariantKind Variant =
708      MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
709    if (Variant == MCSymbolRefExpr::VK_Invalid)
710      return TokError("invalid variant '" + getTok().getIdentifier() + "'");
711
712    const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
713    if (!ModifiedRes) {
714      return TokError("invalid modifier '" + getTok().getIdentifier() +
715                      "' (no symbols present)");
716      return true;
717    }
718
719    Res = ModifiedRes;
720    Lex();
721  }
722
723  // Try to constant fold it up front, if possible.
724  int64_t Value;
725  if (Res->EvaluateAsAbsolute(Value))
726    Res = MCConstantExpr::Create(Value, getContext());
727
728  return false;
729}
730
731bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
732  Res = 0;
733  return ParseParenExpr(Res, EndLoc) ||
734         ParseBinOpRHS(1, Res, EndLoc);
735}
736
737bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
738  const MCExpr *Expr;
739
740  SMLoc StartLoc = Lexer.getLoc();
741  if (ParseExpression(Expr))
742    return true;
743
744  if (!Expr->EvaluateAsAbsolute(Res))
745    return Error(StartLoc, "expected absolute expression");
746
747  return false;
748}
749
750static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
751                                   MCBinaryExpr::Opcode &Kind) {
752  switch (K) {
753  default:
754    return 0;    // not a binop.
755
756    // Lowest Precedence: &&, ||, @
757  case AsmToken::AmpAmp:
758    Kind = MCBinaryExpr::LAnd;
759    return 1;
760  case AsmToken::PipePipe:
761    Kind = MCBinaryExpr::LOr;
762    return 1;
763
764
765    // Low Precedence: |, &, ^
766    //
767    // FIXME: gas seems to support '!' as an infix operator?
768  case AsmToken::Pipe:
769    Kind = MCBinaryExpr::Or;
770    return 2;
771  case AsmToken::Caret:
772    Kind = MCBinaryExpr::Xor;
773    return 2;
774  case AsmToken::Amp:
775    Kind = MCBinaryExpr::And;
776    return 2;
777
778    // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
779  case AsmToken::EqualEqual:
780    Kind = MCBinaryExpr::EQ;
781    return 3;
782  case AsmToken::ExclaimEqual:
783  case AsmToken::LessGreater:
784    Kind = MCBinaryExpr::NE;
785    return 3;
786  case AsmToken::Less:
787    Kind = MCBinaryExpr::LT;
788    return 3;
789  case AsmToken::LessEqual:
790    Kind = MCBinaryExpr::LTE;
791    return 3;
792  case AsmToken::Greater:
793    Kind = MCBinaryExpr::GT;
794    return 3;
795  case AsmToken::GreaterEqual:
796    Kind = MCBinaryExpr::GTE;
797    return 3;
798
799    // High Intermediate Precedence: +, -
800  case AsmToken::Plus:
801    Kind = MCBinaryExpr::Add;
802    return 4;
803  case AsmToken::Minus:
804    Kind = MCBinaryExpr::Sub;
805    return 4;
806
807    // Highest Precedence: *, /, %, <<, >>
808  case AsmToken::Star:
809    Kind = MCBinaryExpr::Mul;
810    return 5;
811  case AsmToken::Slash:
812    Kind = MCBinaryExpr::Div;
813    return 5;
814  case AsmToken::Percent:
815    Kind = MCBinaryExpr::Mod;
816    return 5;
817  case AsmToken::LessLess:
818    Kind = MCBinaryExpr::Shl;
819    return 5;
820  case AsmToken::GreaterGreater:
821    Kind = MCBinaryExpr::Shr;
822    return 5;
823  }
824}
825
826
827/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
828/// Res contains the LHS of the expression on input.
829bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
830                              SMLoc &EndLoc) {
831  while (1) {
832    MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
833    unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
834
835    // If the next token is lower precedence than we are allowed to eat, return
836    // successfully with what we ate already.
837    if (TokPrec < Precedence)
838      return false;
839
840    Lex();
841
842    // Eat the next primary expression.
843    const MCExpr *RHS;
844    if (ParsePrimaryExpr(RHS, EndLoc)) return true;
845
846    // If BinOp binds less tightly with RHS than the operator after RHS, let
847    // the pending operator take RHS as its LHS.
848    MCBinaryExpr::Opcode Dummy;
849    unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
850    if (TokPrec < NextTokPrec) {
851      if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
852    }
853
854    // Merge LHS and RHS according to operator.
855    Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
856  }
857}
858
859
860
861
862/// ParseStatement:
863///   ::= EndOfStatement
864///   ::= Label* Directive ...Operands... EndOfStatement
865///   ::= Label* Identifier OperandList* EndOfStatement
866bool AsmParser::ParseStatement() {
867  if (Lexer.is(AsmToken::EndOfStatement)) {
868    Out.AddBlankLine();
869    Lex();
870    return false;
871  }
872
873  // Statements always start with an identifier or are a full line comment.
874  AsmToken ID = getTok();
875  SMLoc IDLoc = ID.getLoc();
876  StringRef IDVal;
877  int64_t LocalLabelVal = -1;
878  // A full line comment is a '#' as the first token.
879  if (Lexer.is(AsmToken::Hash)) {
880    EatToEndOfStatement();
881    return false;
882  }
883
884  // Allow an integer followed by a ':' as a directional local label.
885  if (Lexer.is(AsmToken::Integer)) {
886    LocalLabelVal = getTok().getIntVal();
887    if (LocalLabelVal < 0) {
888      if (!TheCondState.Ignore)
889        return TokError("unexpected token at start of statement");
890      IDVal = "";
891    }
892    else {
893      IDVal = getTok().getString();
894      Lex(); // Consume the integer token to be used as an identifier token.
895      if (Lexer.getKind() != AsmToken::Colon) {
896        if (!TheCondState.Ignore)
897          return TokError("unexpected token at start of statement");
898      }
899    }
900  } else if (ParseIdentifier(IDVal)) {
901    if (!TheCondState.Ignore)
902      return TokError("unexpected token at start of statement");
903    IDVal = "";
904  }
905
906  // Handle conditional assembly here before checking for skipping.  We
907  // have to do this so that .endif isn't skipped in a ".if 0" block for
908  // example.
909  if (IDVal == ".if")
910    return ParseDirectiveIf(IDLoc);
911  if (IDVal == ".ifdef")
912    return ParseDirectiveIfdef(IDLoc, true);
913  if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
914    return ParseDirectiveIfdef(IDLoc, false);
915  if (IDVal == ".elseif")
916    return ParseDirectiveElseIf(IDLoc);
917  if (IDVal == ".else")
918    return ParseDirectiveElse(IDLoc);
919  if (IDVal == ".endif")
920    return ParseDirectiveEndIf(IDLoc);
921
922  // If we are in a ".if 0" block, ignore this statement.
923  if (TheCondState.Ignore) {
924    EatToEndOfStatement();
925    return false;
926  }
927
928  // FIXME: Recurse on local labels?
929
930  // See what kind of statement we have.
931  switch (Lexer.getKind()) {
932  case AsmToken::Colon: {
933    CheckForValidSection();
934
935    // identifier ':'   -> Label.
936    Lex();
937
938    // Diagnose attempt to use a variable as a label.
939    //
940    // FIXME: Diagnostics. Note the location of the definition as a label.
941    // FIXME: This doesn't diagnose assignment to a symbol which has been
942    // implicitly marked as external.
943    MCSymbol *Sym;
944    if (LocalLabelVal == -1)
945      Sym = getContext().GetOrCreateSymbol(IDVal);
946    else
947      Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
948    if (!Sym->isUndefined() || Sym->isVariable())
949      return Error(IDLoc, "invalid symbol redefinition");
950
951    // Emit the label.
952    Out.EmitLabel(Sym);
953
954    // Consume any end of statement token, if present, to avoid spurious
955    // AddBlankLine calls().
956    if (Lexer.is(AsmToken::EndOfStatement)) {
957      Lex();
958      if (Lexer.is(AsmToken::Eof))
959        return false;
960    }
961
962    return ParseStatement();
963  }
964
965  case AsmToken::Equal:
966    // identifier '=' ... -> assignment statement
967    Lex();
968
969    return ParseAssignment(IDVal, true);
970
971  default: // Normal instruction or directive.
972    break;
973  }
974
975  // If macros are enabled, check to see if this is a macro instantiation.
976  if (MacrosEnabled)
977    if (const Macro *M = MacroMap.lookup(IDVal))
978      return HandleMacroEntry(IDVal, IDLoc, M);
979
980  // Otherwise, we have a normal instruction or directive.
981  if (IDVal[0] == '.') {
982    // Assembler features
983    if (IDVal == ".set" || IDVal == ".equ")
984      return ParseDirectiveSet(IDVal, true);
985    if (IDVal == ".equiv")
986      return ParseDirectiveSet(IDVal, false);
987
988    // Data directives
989
990    if (IDVal == ".ascii")
991      return ParseDirectiveAscii(IDVal, false);
992    if (IDVal == ".asciz" || IDVal == ".string")
993      return ParseDirectiveAscii(IDVal, true);
994
995    if (IDVal == ".byte")
996      return ParseDirectiveValue(1);
997    if (IDVal == ".short")
998      return ParseDirectiveValue(2);
999    if (IDVal == ".value")
1000      return ParseDirectiveValue(2);
1001    if (IDVal == ".2byte")
1002      return ParseDirectiveValue(2);
1003    if (IDVal == ".long")
1004      return ParseDirectiveValue(4);
1005    if (IDVal == ".int")
1006      return ParseDirectiveValue(4);
1007    if (IDVal == ".4byte")
1008      return ParseDirectiveValue(4);
1009    if (IDVal == ".quad")
1010      return ParseDirectiveValue(8);
1011    if (IDVal == ".8byte")
1012      return ParseDirectiveValue(8);
1013    if (IDVal == ".single" || IDVal == ".float")
1014      return ParseDirectiveRealValue(APFloat::IEEEsingle);
1015    if (IDVal == ".double")
1016      return ParseDirectiveRealValue(APFloat::IEEEdouble);
1017
1018    if (IDVal == ".align") {
1019      bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1020      return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1021    }
1022    if (IDVal == ".align32") {
1023      bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1024      return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1025    }
1026    if (IDVal == ".balign")
1027      return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1028    if (IDVal == ".balignw")
1029      return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1030    if (IDVal == ".balignl")
1031      return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1032    if (IDVal == ".p2align")
1033      return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1034    if (IDVal == ".p2alignw")
1035      return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1036    if (IDVal == ".p2alignl")
1037      return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1038
1039    if (IDVal == ".org")
1040      return ParseDirectiveOrg();
1041
1042    if (IDVal == ".fill")
1043      return ParseDirectiveFill();
1044    if (IDVal == ".space")
1045      return ParseDirectiveSpace();
1046    if (IDVal == ".zero")
1047      return ParseDirectiveZero();
1048
1049    // Symbol attribute directives
1050
1051    if (IDVal == ".globl" || IDVal == ".global")
1052      return ParseDirectiveSymbolAttribute(MCSA_Global);
1053    // ELF only? Should it be here?
1054    if (IDVal == ".local")
1055      return ParseDirectiveSymbolAttribute(MCSA_Local);
1056    if (IDVal == ".hidden")
1057      return ParseDirectiveSymbolAttribute(MCSA_Hidden);
1058    if (IDVal == ".indirect_symbol")
1059      return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
1060    if (IDVal == ".internal")
1061      return ParseDirectiveSymbolAttribute(MCSA_Internal);
1062    if (IDVal == ".lazy_reference")
1063      return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
1064    if (IDVal == ".no_dead_strip")
1065      return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1066    if (IDVal == ".symbol_resolver")
1067      return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1068    if (IDVal == ".private_extern")
1069      return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1070    if (IDVal == ".protected")
1071      return ParseDirectiveSymbolAttribute(MCSA_Protected);
1072    if (IDVal == ".reference")
1073      return ParseDirectiveSymbolAttribute(MCSA_Reference);
1074    if (IDVal == ".weak")
1075      return ParseDirectiveSymbolAttribute(MCSA_Weak);
1076    if (IDVal == ".weak_definition")
1077      return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1078    if (IDVal == ".weak_reference")
1079      return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
1080    if (IDVal == ".weak_def_can_be_hidden")
1081      return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1082
1083    if (IDVal == ".comm")
1084      return ParseDirectiveComm(/*IsLocal=*/false);
1085    if (IDVal == ".lcomm")
1086      return ParseDirectiveComm(/*IsLocal=*/true);
1087
1088    if (IDVal == ".abort")
1089      return ParseDirectiveAbort();
1090    if (IDVal == ".include")
1091      return ParseDirectiveInclude();
1092
1093    if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
1094      return TokError(Twine(IDVal) + " not supported yet");
1095
1096    // Look up the handler in the handler table.
1097    std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1098      DirectiveMap.lookup(IDVal);
1099    if (Handler.first)
1100      return (*Handler.second)(Handler.first, IDVal, IDLoc);
1101
1102    // Target hook for parsing target specific directives.
1103    if (!getTargetParser().ParseDirective(ID))
1104      return false;
1105
1106    Warning(IDLoc, "ignoring directive for now");
1107    EatToEndOfStatement();
1108    return false;
1109  }
1110
1111  CheckForValidSection();
1112
1113  // Canonicalize the opcode to lower case.
1114  SmallString<128> Opcode;
1115  for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1116    Opcode.push_back(tolower(IDVal[i]));
1117
1118  SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
1119  bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
1120                                                     ParsedOperands);
1121
1122  // Dump the parsed representation, if requested.
1123  if (getShowParsedOperands()) {
1124    SmallString<256> Str;
1125    raw_svector_ostream OS(Str);
1126    OS << "parsed instruction: [";
1127    for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1128      if (i != 0)
1129        OS << ", ";
1130      ParsedOperands[i]->dump(OS);
1131    }
1132    OS << "]";
1133
1134    PrintMessage(IDLoc, OS.str(), "note");
1135  }
1136
1137  // If parsing succeeded, match the instruction.
1138  if (!HadError)
1139    HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1140                                                         Out);
1141
1142  // Free any parsed operands.
1143  for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1144    delete ParsedOperands[i];
1145
1146  // Don't skip the rest of the line, the instruction parser is responsible for
1147  // that.
1148  return false;
1149}
1150
1151MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1152                                   const std::vector<std::vector<AsmToken> > &A)
1153  : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1154{
1155  // Macro instantiation is lexical, unfortunately. We construct a new buffer
1156  // to hold the macro body with substitutions.
1157  SmallString<256> Buf;
1158  raw_svector_ostream OS(Buf);
1159
1160  StringRef Body = M->Body;
1161  while (!Body.empty()) {
1162    // Scan for the next substitution.
1163    std::size_t End = Body.size(), Pos = 0;
1164    for (; Pos != End; ++Pos) {
1165      // Check for a substitution or escape.
1166      if (Body[Pos] != '$' || Pos + 1 == End)
1167        continue;
1168
1169      char Next = Body[Pos + 1];
1170      if (Next == '$' || Next == 'n' || isdigit(Next))
1171        break;
1172    }
1173
1174    // Add the prefix.
1175    OS << Body.slice(0, Pos);
1176
1177    // Check if we reached the end.
1178    if (Pos == End)
1179      break;
1180
1181    switch (Body[Pos+1]) {
1182       // $$ => $
1183    case '$':
1184      OS << '$';
1185      break;
1186
1187      // $n => number of arguments
1188    case 'n':
1189      OS << A.size();
1190      break;
1191
1192       // $[0-9] => argument
1193    default: {
1194      // Missing arguments are ignored.
1195      unsigned Index = Body[Pos+1] - '0';
1196      if (Index >= A.size())
1197        break;
1198
1199      // Otherwise substitute with the token values, with spaces eliminated.
1200      for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1201             ie = A[Index].end(); it != ie; ++it)
1202        OS << it->getString();
1203      break;
1204    }
1205    }
1206
1207    // Update the scan point.
1208    Body = Body.substr(Pos + 2);
1209  }
1210
1211  // We include the .endmacro in the buffer as our queue to exit the macro
1212  // instantiation.
1213  OS << ".endmacro\n";
1214
1215  Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
1216}
1217
1218bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1219                                 const Macro *M) {
1220  // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1221  // this, although we should protect against infinite loops.
1222  if (ActiveMacros.size() == 20)
1223    return TokError("macros cannot be nested more than 20 levels deep");
1224
1225  // Parse the macro instantiation arguments.
1226  std::vector<std::vector<AsmToken> > MacroArguments;
1227  MacroArguments.push_back(std::vector<AsmToken>());
1228  unsigned ParenLevel = 0;
1229  for (;;) {
1230    if (Lexer.is(AsmToken::Eof))
1231      return TokError("unexpected token in macro instantiation");
1232    if (Lexer.is(AsmToken::EndOfStatement))
1233      break;
1234
1235    // If we aren't inside parentheses and this is a comma, start a new token
1236    // list.
1237    if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1238      MacroArguments.push_back(std::vector<AsmToken>());
1239    } else {
1240      // Adjust the current parentheses level.
1241      if (Lexer.is(AsmToken::LParen))
1242        ++ParenLevel;
1243      else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1244        --ParenLevel;
1245
1246      // Append the token to the current argument list.
1247      MacroArguments.back().push_back(getTok());
1248    }
1249    Lex();
1250  }
1251
1252  // Create the macro instantiation object and add to the current macro
1253  // instantiation stack.
1254  MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
1255                                                  getTok().getLoc(),
1256                                                  MacroArguments);
1257  ActiveMacros.push_back(MI);
1258
1259  // Jump to the macro instantiation and prime the lexer.
1260  CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1261  Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1262  Lex();
1263
1264  return false;
1265}
1266
1267void AsmParser::HandleMacroExit() {
1268  // Jump to the EndOfStatement we should return to, and consume it.
1269  JumpToLoc(ActiveMacros.back()->ExitLoc);
1270  Lex();
1271
1272  // Pop the instantiation entry.
1273  delete ActiveMacros.back();
1274  ActiveMacros.pop_back();
1275}
1276
1277static void MarkUsed(const MCExpr *Value) {
1278  switch (Value->getKind()) {
1279  case MCExpr::Binary:
1280    MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1281    MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1282    break;
1283  case MCExpr::Target:
1284  case MCExpr::Constant:
1285    break;
1286  case MCExpr::SymbolRef: {
1287    static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1288    break;
1289  }
1290  case MCExpr::Unary:
1291    MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1292    break;
1293  }
1294}
1295
1296bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
1297  // FIXME: Use better location, we should use proper tokens.
1298  SMLoc EqualLoc = Lexer.getLoc();
1299
1300  const MCExpr *Value;
1301  if (ParseExpression(Value))
1302    return true;
1303
1304  MarkUsed(Value);
1305
1306  if (Lexer.isNot(AsmToken::EndOfStatement))
1307    return TokError("unexpected token in assignment");
1308
1309  // Eat the end of statement marker.
1310  Lex();
1311
1312  // Validate that the LHS is allowed to be a variable (either it has not been
1313  // used as a symbol, or it is an absolute symbol).
1314  MCSymbol *Sym = getContext().LookupSymbol(Name);
1315  if (Sym) {
1316    // Diagnose assignment to a label.
1317    //
1318    // FIXME: Diagnostics. Note the location of the definition as a label.
1319    // FIXME: Diagnose assignment to protected identifier (e.g., register name).
1320    if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
1321      ; // Allow redefinitions of undefined symbols only used in directives.
1322    else if (!Sym->isUndefined() && (!Sym->isAbsolute() || !allow_redef))
1323      return Error(EqualLoc, "redefinition of '" + Name + "'");
1324    else if (!Sym->isVariable())
1325      return Error(EqualLoc, "invalid assignment to '" + Name + "'");
1326    else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
1327      return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1328                   Name + "'");
1329
1330    // Don't count these checks as uses.
1331    Sym->setUsed(false);
1332  } else
1333    Sym = getContext().GetOrCreateSymbol(Name);
1334
1335  // FIXME: Handle '.'.
1336
1337  // Do the assignment.
1338  Out.EmitAssignment(Sym, Value);
1339
1340  return false;
1341}
1342
1343/// ParseIdentifier:
1344///   ::= identifier
1345///   ::= string
1346bool AsmParser::ParseIdentifier(StringRef &Res) {
1347  // The assembler has relaxed rules for accepting identifiers, in particular we
1348  // allow things like '.globl $foo', which would normally be separate
1349  // tokens. At this level, we have already lexed so we cannot (currently)
1350  // handle this as a context dependent token, instead we detect adjacent tokens
1351  // and return the combined identifier.
1352  if (Lexer.is(AsmToken::Dollar)) {
1353    SMLoc DollarLoc = getLexer().getLoc();
1354
1355    // Consume the dollar sign, and check for a following identifier.
1356    Lex();
1357    if (Lexer.isNot(AsmToken::Identifier))
1358      return true;
1359
1360    // We have a '$' followed by an identifier, make sure they are adjacent.
1361    if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1362      return true;
1363
1364    // Construct the joined identifier and consume the token.
1365    Res = StringRef(DollarLoc.getPointer(),
1366                    getTok().getIdentifier().size() + 1);
1367    Lex();
1368    return false;
1369  }
1370
1371  if (Lexer.isNot(AsmToken::Identifier) &&
1372      Lexer.isNot(AsmToken::String))
1373    return true;
1374
1375  Res = getTok().getIdentifier();
1376
1377  Lex(); // Consume the identifier token.
1378
1379  return false;
1380}
1381
1382/// ParseDirectiveSet:
1383///   ::= .equ identifier ',' expression
1384///   ::= .equiv identifier ',' expression
1385///   ::= .set identifier ',' expression
1386bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
1387  StringRef Name;
1388
1389  if (ParseIdentifier(Name))
1390    return TokError("expected identifier after '" + Twine(IDVal) + "'");
1391
1392  if (getLexer().isNot(AsmToken::Comma))
1393    return TokError("unexpected token in '" + Twine(IDVal) + "'");
1394  Lex();
1395
1396  return ParseAssignment(Name, allow_redef);
1397}
1398
1399bool AsmParser::ParseEscapedString(std::string &Data) {
1400  assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
1401
1402  Data = "";
1403  StringRef Str = getTok().getStringContents();
1404  for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1405    if (Str[i] != '\\') {
1406      Data += Str[i];
1407      continue;
1408    }
1409
1410    // Recognize escaped characters. Note that this escape semantics currently
1411    // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1412    ++i;
1413    if (i == e)
1414      return TokError("unexpected backslash at end of string");
1415
1416    // Recognize octal sequences.
1417    if ((unsigned) (Str[i] - '0') <= 7) {
1418      // Consume up to three octal characters.
1419      unsigned Value = Str[i] - '0';
1420
1421      if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1422        ++i;
1423        Value = Value * 8 + (Str[i] - '0');
1424
1425        if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1426          ++i;
1427          Value = Value * 8 + (Str[i] - '0');
1428        }
1429      }
1430
1431      if (Value > 255)
1432        return TokError("invalid octal escape sequence (out of range)");
1433
1434      Data += (unsigned char) Value;
1435      continue;
1436    }
1437
1438    // Otherwise recognize individual escapes.
1439    switch (Str[i]) {
1440    default:
1441      // Just reject invalid escape sequences for now.
1442      return TokError("invalid escape sequence (unrecognized character)");
1443
1444    case 'b': Data += '\b'; break;
1445    case 'f': Data += '\f'; break;
1446    case 'n': Data += '\n'; break;
1447    case 'r': Data += '\r'; break;
1448    case 't': Data += '\t'; break;
1449    case '"': Data += '"'; break;
1450    case '\\': Data += '\\'; break;
1451    }
1452  }
1453
1454  return false;
1455}
1456
1457/// ParseDirectiveAscii:
1458///   ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1459bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
1460  if (getLexer().isNot(AsmToken::EndOfStatement)) {
1461    CheckForValidSection();
1462
1463    for (;;) {
1464      if (getLexer().isNot(AsmToken::String))
1465        return TokError("expected string in '" + Twine(IDVal) + "' directive");
1466
1467      std::string Data;
1468      if (ParseEscapedString(Data))
1469        return true;
1470
1471      getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
1472      if (ZeroTerminated)
1473        getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1474
1475      Lex();
1476
1477      if (getLexer().is(AsmToken::EndOfStatement))
1478        break;
1479
1480      if (getLexer().isNot(AsmToken::Comma))
1481        return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
1482      Lex();
1483    }
1484  }
1485
1486  Lex();
1487  return false;
1488}
1489
1490/// ParseDirectiveValue
1491///  ::= (.byte | .short | ... ) [ expression (, expression)* ]
1492bool AsmParser::ParseDirectiveValue(unsigned Size) {
1493  if (getLexer().isNot(AsmToken::EndOfStatement)) {
1494    CheckForValidSection();
1495
1496    for (;;) {
1497      const MCExpr *Value;
1498      if (ParseExpression(Value))
1499        return true;
1500
1501      // Special case constant expressions to match code generator.
1502      if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
1503        getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
1504      else
1505        getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
1506
1507      if (getLexer().is(AsmToken::EndOfStatement))
1508        break;
1509
1510      // FIXME: Improve diagnostic.
1511      if (getLexer().isNot(AsmToken::Comma))
1512        return TokError("unexpected token in directive");
1513      Lex();
1514    }
1515  }
1516
1517  Lex();
1518  return false;
1519}
1520
1521/// ParseDirectiveRealValue
1522///  ::= (.single | .double) [ expression (, expression)* ]
1523bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1524  if (getLexer().isNot(AsmToken::EndOfStatement)) {
1525    CheckForValidSection();
1526
1527    for (;;) {
1528      // We don't truly support arithmetic on floating point expressions, so we
1529      // have to manually parse unary prefixes.
1530      bool IsNeg = false;
1531      if (getLexer().is(AsmToken::Minus)) {
1532        Lex();
1533        IsNeg = true;
1534      } else if (getLexer().is(AsmToken::Plus))
1535        Lex();
1536
1537      if (getLexer().isNot(AsmToken::Integer) &&
1538          getLexer().isNot(AsmToken::Real))
1539        return TokError("unexpected token in directive");
1540
1541      // Convert to an APFloat.
1542      APFloat Value(Semantics);
1543      if (Value.convertFromString(getTok().getString(),
1544                                  APFloat::rmNearestTiesToEven) ==
1545          APFloat::opInvalidOp)
1546        return TokError("invalid floating point literal");
1547      if (IsNeg)
1548        Value.changeSign();
1549
1550      // Consume the numeric token.
1551      Lex();
1552
1553      // Emit the value as an integer.
1554      APInt AsInt = Value.bitcastToAPInt();
1555      getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1556                                 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1557
1558      if (getLexer().is(AsmToken::EndOfStatement))
1559        break;
1560
1561      if (getLexer().isNot(AsmToken::Comma))
1562        return TokError("unexpected token in directive");
1563      Lex();
1564    }
1565  }
1566
1567  Lex();
1568  return false;
1569}
1570
1571/// ParseDirectiveSpace
1572///  ::= .space expression [ , expression ]
1573bool AsmParser::ParseDirectiveSpace() {
1574  CheckForValidSection();
1575
1576  int64_t NumBytes;
1577  if (ParseAbsoluteExpression(NumBytes))
1578    return true;
1579
1580  int64_t FillExpr = 0;
1581  if (getLexer().isNot(AsmToken::EndOfStatement)) {
1582    if (getLexer().isNot(AsmToken::Comma))
1583      return TokError("unexpected token in '.space' directive");
1584    Lex();
1585
1586    if (ParseAbsoluteExpression(FillExpr))
1587      return true;
1588
1589    if (getLexer().isNot(AsmToken::EndOfStatement))
1590      return TokError("unexpected token in '.space' directive");
1591  }
1592
1593  Lex();
1594
1595  if (NumBytes <= 0)
1596    return TokError("invalid number of bytes in '.space' directive");
1597
1598  // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1599  getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
1600
1601  return false;
1602}
1603
1604/// ParseDirectiveZero
1605///  ::= .zero expression
1606bool AsmParser::ParseDirectiveZero() {
1607  CheckForValidSection();
1608
1609  int64_t NumBytes;
1610  if (ParseAbsoluteExpression(NumBytes))
1611    return true;
1612
1613  int64_t Val = 0;
1614  if (getLexer().is(AsmToken::Comma)) {
1615    Lex();
1616    if (ParseAbsoluteExpression(Val))
1617      return true;
1618  }
1619
1620  if (getLexer().isNot(AsmToken::EndOfStatement))
1621    return TokError("unexpected token in '.zero' directive");
1622
1623  Lex();
1624
1625  getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
1626
1627  return false;
1628}
1629
1630/// ParseDirectiveFill
1631///  ::= .fill expression , expression , expression
1632bool AsmParser::ParseDirectiveFill() {
1633  CheckForValidSection();
1634
1635  int64_t NumValues;
1636  if (ParseAbsoluteExpression(NumValues))
1637    return true;
1638
1639  if (getLexer().isNot(AsmToken::Comma))
1640    return TokError("unexpected token in '.fill' directive");
1641  Lex();
1642
1643  int64_t FillSize;
1644  if (ParseAbsoluteExpression(FillSize))
1645    return true;
1646
1647  if (getLexer().isNot(AsmToken::Comma))
1648    return TokError("unexpected token in '.fill' directive");
1649  Lex();
1650
1651  int64_t FillExpr;
1652  if (ParseAbsoluteExpression(FillExpr))
1653    return true;
1654
1655  if (getLexer().isNot(AsmToken::EndOfStatement))
1656    return TokError("unexpected token in '.fill' directive");
1657
1658  Lex();
1659
1660  if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1661    return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1662
1663  for (uint64_t i = 0, e = NumValues; i != e; ++i)
1664    getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
1665
1666  return false;
1667}
1668
1669/// ParseDirectiveOrg
1670///  ::= .org expression [ , expression ]
1671bool AsmParser::ParseDirectiveOrg() {
1672  CheckForValidSection();
1673
1674  const MCExpr *Offset;
1675  if (ParseExpression(Offset))
1676    return true;
1677
1678  // Parse optional fill expression.
1679  int64_t FillExpr = 0;
1680  if (getLexer().isNot(AsmToken::EndOfStatement)) {
1681    if (getLexer().isNot(AsmToken::Comma))
1682      return TokError("unexpected token in '.org' directive");
1683    Lex();
1684
1685    if (ParseAbsoluteExpression(FillExpr))
1686      return true;
1687
1688    if (getLexer().isNot(AsmToken::EndOfStatement))
1689      return TokError("unexpected token in '.org' directive");
1690  }
1691
1692  Lex();
1693
1694  // FIXME: Only limited forms of relocatable expressions are accepted here, it
1695  // has to be relative to the current section.
1696  getStreamer().EmitValueToOffset(Offset, FillExpr);
1697
1698  return false;
1699}
1700
1701/// ParseDirectiveAlign
1702///  ::= {.align, ...} expression [ , expression [ , expression ]]
1703bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1704  CheckForValidSection();
1705
1706  SMLoc AlignmentLoc = getLexer().getLoc();
1707  int64_t Alignment;
1708  if (ParseAbsoluteExpression(Alignment))
1709    return true;
1710
1711  SMLoc MaxBytesLoc;
1712  bool HasFillExpr = false;
1713  int64_t FillExpr = 0;
1714  int64_t MaxBytesToFill = 0;
1715  if (getLexer().isNot(AsmToken::EndOfStatement)) {
1716    if (getLexer().isNot(AsmToken::Comma))
1717      return TokError("unexpected token in directive");
1718    Lex();
1719
1720    // The fill expression can be omitted while specifying a maximum number of
1721    // alignment bytes, e.g:
1722    //  .align 3,,4
1723    if (getLexer().isNot(AsmToken::Comma)) {
1724      HasFillExpr = true;
1725      if (ParseAbsoluteExpression(FillExpr))
1726        return true;
1727    }
1728
1729    if (getLexer().isNot(AsmToken::EndOfStatement)) {
1730      if (getLexer().isNot(AsmToken::Comma))
1731        return TokError("unexpected token in directive");
1732      Lex();
1733
1734      MaxBytesLoc = getLexer().getLoc();
1735      if (ParseAbsoluteExpression(MaxBytesToFill))
1736        return true;
1737
1738      if (getLexer().isNot(AsmToken::EndOfStatement))
1739        return TokError("unexpected token in directive");
1740    }
1741  }
1742
1743  Lex();
1744
1745  if (!HasFillExpr)
1746    FillExpr = 0;
1747
1748  // Compute alignment in bytes.
1749  if (IsPow2) {
1750    // FIXME: Diagnose overflow.
1751    if (Alignment >= 32) {
1752      Error(AlignmentLoc, "invalid alignment value");
1753      Alignment = 31;
1754    }
1755
1756    Alignment = 1ULL << Alignment;
1757  }
1758
1759  // Diagnose non-sensical max bytes to align.
1760  if (MaxBytesLoc.isValid()) {
1761    if (MaxBytesToFill < 1) {
1762      Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1763            "many bytes, ignoring maximum bytes expression");
1764      MaxBytesToFill = 0;
1765    }
1766
1767    if (MaxBytesToFill >= Alignment) {
1768      Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1769              "has no effect");
1770      MaxBytesToFill = 0;
1771    }
1772  }
1773
1774  // Check whether we should use optimal code alignment for this .align
1775  // directive.
1776  bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
1777  if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1778      ValueSize == 1 && UseCodeAlign) {
1779    getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
1780  } else {
1781    // FIXME: Target specific behavior about how the "extra" bytes are filled.
1782    getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1783                                       MaxBytesToFill);
1784  }
1785
1786  return false;
1787}
1788
1789/// ParseDirectiveSymbolAttribute
1790///  ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1791bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
1792  if (getLexer().isNot(AsmToken::EndOfStatement)) {
1793    for (;;) {
1794      StringRef Name;
1795
1796      if (ParseIdentifier(Name))
1797        return TokError("expected identifier in directive");
1798
1799      MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
1800
1801      getStreamer().EmitSymbolAttribute(Sym, Attr);
1802
1803      if (getLexer().is(AsmToken::EndOfStatement))
1804        break;
1805
1806      if (getLexer().isNot(AsmToken::Comma))
1807        return TokError("unexpected token in directive");
1808      Lex();
1809    }
1810  }
1811
1812  Lex();
1813  return false;
1814}
1815
1816/// ParseDirectiveComm
1817///  ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1818bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1819  CheckForValidSection();
1820
1821  SMLoc IDLoc = getLexer().getLoc();
1822  StringRef Name;
1823  if (ParseIdentifier(Name))
1824    return TokError("expected identifier in directive");
1825
1826  // Handle the identifier as the key symbol.
1827  MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
1828
1829  if (getLexer().isNot(AsmToken::Comma))
1830    return TokError("unexpected token in directive");
1831  Lex();
1832
1833  int64_t Size;
1834  SMLoc SizeLoc = getLexer().getLoc();
1835  if (ParseAbsoluteExpression(Size))
1836    return true;
1837
1838  int64_t Pow2Alignment = 0;
1839  SMLoc Pow2AlignmentLoc;
1840  if (getLexer().is(AsmToken::Comma)) {
1841    Lex();
1842    Pow2AlignmentLoc = getLexer().getLoc();
1843    if (ParseAbsoluteExpression(Pow2Alignment))
1844      return true;
1845
1846    // If this target takes alignments in bytes (not log) validate and convert.
1847    if (Lexer.getMAI().getAlignmentIsInBytes()) {
1848      if (!isPowerOf2_64(Pow2Alignment))
1849        return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1850      Pow2Alignment = Log2_64(Pow2Alignment);
1851    }
1852  }
1853
1854  if (getLexer().isNot(AsmToken::EndOfStatement))
1855    return TokError("unexpected token in '.comm' or '.lcomm' directive");
1856
1857  Lex();
1858
1859  // NOTE: a size of zero for a .comm should create a undefined symbol
1860  // but a size of .lcomm creates a bss symbol of size zero.
1861  if (Size < 0)
1862    return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1863                 "be less than zero");
1864
1865  // NOTE: The alignment in the directive is a power of 2 value, the assembler
1866  // may internally end up wanting an alignment in bytes.
1867  // FIXME: Diagnose overflow.
1868  if (Pow2Alignment < 0)
1869    return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1870                 "alignment, can't be less than zero");
1871
1872  if (!Sym->isUndefined())
1873    return Error(IDLoc, "invalid symbol redefinition");
1874
1875  // '.lcomm' is equivalent to '.zerofill'.
1876  // Create the Symbol as a common or local common with Size and Pow2Alignment
1877  if (IsLocal) {
1878    getStreamer().EmitZerofill(Ctx.getMachOSection(
1879                                 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1880                                 0, SectionKind::getBSS()),
1881                               Sym, Size, 1 << Pow2Alignment);
1882    return false;
1883  }
1884
1885  getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
1886  return false;
1887}
1888
1889/// ParseDirectiveAbort
1890///  ::= .abort [... message ...]
1891bool AsmParser::ParseDirectiveAbort() {
1892  // FIXME: Use loc from directive.
1893  SMLoc Loc = getLexer().getLoc();
1894
1895  StringRef Str = ParseStringToEndOfStatement();
1896  if (getLexer().isNot(AsmToken::EndOfStatement))
1897    return TokError("unexpected token in '.abort' directive");
1898
1899  Lex();
1900
1901  if (Str.empty())
1902    Error(Loc, ".abort detected. Assembly stopping.");
1903  else
1904    Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1905  // FIXME: Actually abort assembly here.
1906
1907  return false;
1908}
1909
1910/// ParseDirectiveInclude
1911///  ::= .include "filename"
1912bool AsmParser::ParseDirectiveInclude() {
1913  if (getLexer().isNot(AsmToken::String))
1914    return TokError("expected string in '.include' directive");
1915
1916  std::string Filename = getTok().getString();
1917  SMLoc IncludeLoc = getLexer().getLoc();
1918  Lex();
1919
1920  if (getLexer().isNot(AsmToken::EndOfStatement))
1921    return TokError("unexpected token in '.include' directive");
1922
1923  // Strip the quotes.
1924  Filename = Filename.substr(1, Filename.size()-2);
1925
1926  // Attempt to switch the lexer to the included file before consuming the end
1927  // of statement to avoid losing it when we switch.
1928  if (EnterIncludeFile(Filename)) {
1929    Error(IncludeLoc, "Could not find include file '" + Filename + "'");
1930    return true;
1931  }
1932
1933  return false;
1934}
1935
1936/// ParseDirectiveIf
1937/// ::= .if expression
1938bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1939  TheCondStack.push_back(TheCondState);
1940  TheCondState.TheCond = AsmCond::IfCond;
1941  if(TheCondState.Ignore) {
1942    EatToEndOfStatement();
1943  }
1944  else {
1945    int64_t ExprValue;
1946    if (ParseAbsoluteExpression(ExprValue))
1947      return true;
1948
1949    if (getLexer().isNot(AsmToken::EndOfStatement))
1950      return TokError("unexpected token in '.if' directive");
1951
1952    Lex();
1953
1954    TheCondState.CondMet = ExprValue;
1955    TheCondState.Ignore = !TheCondState.CondMet;
1956  }
1957
1958  return false;
1959}
1960
1961bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
1962  StringRef Name;
1963  TheCondStack.push_back(TheCondState);
1964  TheCondState.TheCond = AsmCond::IfCond;
1965
1966  if (TheCondState.Ignore) {
1967    EatToEndOfStatement();
1968  } else {
1969    if (ParseIdentifier(Name))
1970      return TokError("expected identifier after '.ifdef'");
1971
1972    Lex();
1973
1974    MCSymbol *Sym = getContext().LookupSymbol(Name);
1975
1976    if (expect_defined)
1977      TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
1978    else
1979      TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
1980    TheCondState.Ignore = !TheCondState.CondMet;
1981  }
1982
1983  return false;
1984}
1985
1986/// ParseDirectiveElseIf
1987/// ::= .elseif expression
1988bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1989  if (TheCondState.TheCond != AsmCond::IfCond &&
1990      TheCondState.TheCond != AsmCond::ElseIfCond)
1991      Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1992                          " an .elseif");
1993  TheCondState.TheCond = AsmCond::ElseIfCond;
1994
1995  bool LastIgnoreState = false;
1996  if (!TheCondStack.empty())
1997      LastIgnoreState = TheCondStack.back().Ignore;
1998  if (LastIgnoreState || TheCondState.CondMet) {
1999    TheCondState.Ignore = true;
2000    EatToEndOfStatement();
2001  }
2002  else {
2003    int64_t ExprValue;
2004    if (ParseAbsoluteExpression(ExprValue))
2005      return true;
2006
2007    if (getLexer().isNot(AsmToken::EndOfStatement))
2008      return TokError("unexpected token in '.elseif' directive");
2009
2010    Lex();
2011    TheCondState.CondMet = ExprValue;
2012    TheCondState.Ignore = !TheCondState.CondMet;
2013  }
2014
2015  return false;
2016}
2017
2018/// ParseDirectiveElse
2019/// ::= .else
2020bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
2021  if (getLexer().isNot(AsmToken::EndOfStatement))
2022    return TokError("unexpected token in '.else' directive");
2023
2024  Lex();
2025
2026  if (TheCondState.TheCond != AsmCond::IfCond &&
2027      TheCondState.TheCond != AsmCond::ElseIfCond)
2028      Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2029                          ".elseif");
2030  TheCondState.TheCond = AsmCond::ElseCond;
2031  bool LastIgnoreState = false;
2032  if (!TheCondStack.empty())
2033    LastIgnoreState = TheCondStack.back().Ignore;
2034  if (LastIgnoreState || TheCondState.CondMet)
2035    TheCondState.Ignore = true;
2036  else
2037    TheCondState.Ignore = false;
2038
2039  return false;
2040}
2041
2042/// ParseDirectiveEndIf
2043/// ::= .endif
2044bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
2045  if (getLexer().isNot(AsmToken::EndOfStatement))
2046    return TokError("unexpected token in '.endif' directive");
2047
2048  Lex();
2049
2050  if ((TheCondState.TheCond == AsmCond::NoCond) ||
2051      TheCondStack.empty())
2052    Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2053                        ".else");
2054  if (!TheCondStack.empty()) {
2055    TheCondState = TheCondStack.back();
2056    TheCondStack.pop_back();
2057  }
2058
2059  return false;
2060}
2061
2062/// ParseDirectiveFile
2063/// ::= .file [number] string
2064bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
2065  // FIXME: I'm not sure what this is.
2066  int64_t FileNumber = -1;
2067  SMLoc FileNumberLoc = getLexer().getLoc();
2068  if (getLexer().is(AsmToken::Integer)) {
2069    FileNumber = getTok().getIntVal();
2070    Lex();
2071
2072    if (FileNumber < 1)
2073      return TokError("file number less than one");
2074  }
2075
2076  if (getLexer().isNot(AsmToken::String))
2077    return TokError("unexpected token in '.file' directive");
2078
2079  StringRef Filename = getTok().getString();
2080  Filename = Filename.substr(1, Filename.size()-2);
2081  Lex();
2082
2083  if (getLexer().isNot(AsmToken::EndOfStatement))
2084    return TokError("unexpected token in '.file' directive");
2085
2086  if (FileNumber == -1)
2087    getStreamer().EmitFileDirective(Filename);
2088  else {
2089    if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
2090      Error(FileNumberLoc, "file number already allocated");
2091  }
2092
2093  return false;
2094}
2095
2096/// ParseDirectiveLine
2097/// ::= .line [number]
2098bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
2099  if (getLexer().isNot(AsmToken::EndOfStatement)) {
2100    if (getLexer().isNot(AsmToken::Integer))
2101      return TokError("unexpected token in '.line' directive");
2102
2103    int64_t LineNumber = getTok().getIntVal();
2104    (void) LineNumber;
2105    Lex();
2106
2107    // FIXME: Do something with the .line.
2108  }
2109
2110  if (getLexer().isNot(AsmToken::EndOfStatement))
2111    return TokError("unexpected token in '.line' directive");
2112
2113  return false;
2114}
2115
2116
2117/// ParseDirectiveLoc
2118/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2119///                                [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2120/// The first number is a file number, must have been previously assigned with
2121/// a .file directive, the second number is the line number and optionally the
2122/// third number is a column position (zero if not specified).  The remaining
2123/// optional items are .loc sub-directives.
2124bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
2125
2126  if (getLexer().isNot(AsmToken::Integer))
2127    return TokError("unexpected token in '.loc' directive");
2128  int64_t FileNumber = getTok().getIntVal();
2129  if (FileNumber < 1)
2130    return TokError("file number less than one in '.loc' directive");
2131  if (!getContext().isValidDwarfFileNumber(FileNumber))
2132    return TokError("unassigned file number in '.loc' directive");
2133  Lex();
2134
2135  int64_t LineNumber = 0;
2136  if (getLexer().is(AsmToken::Integer)) {
2137    LineNumber = getTok().getIntVal();
2138    if (LineNumber < 1)
2139      return TokError("line number less than one in '.loc' directive");
2140    Lex();
2141  }
2142
2143  int64_t ColumnPos = 0;
2144  if (getLexer().is(AsmToken::Integer)) {
2145    ColumnPos = getTok().getIntVal();
2146    if (ColumnPos < 0)
2147      return TokError("column position less than zero in '.loc' directive");
2148    Lex();
2149  }
2150
2151  unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2152  unsigned Isa = 0;
2153  int64_t Discriminator = 0;
2154  if (getLexer().isNot(AsmToken::EndOfStatement)) {
2155    for (;;) {
2156      if (getLexer().is(AsmToken::EndOfStatement))
2157        break;
2158
2159      StringRef Name;
2160      SMLoc Loc = getTok().getLoc();
2161      if (getParser().ParseIdentifier(Name))
2162        return TokError("unexpected token in '.loc' directive");
2163
2164      if (Name == "basic_block")
2165        Flags |= DWARF2_FLAG_BASIC_BLOCK;
2166      else if (Name == "prologue_end")
2167        Flags |= DWARF2_FLAG_PROLOGUE_END;
2168      else if (Name == "epilogue_begin")
2169        Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2170      else if (Name == "is_stmt") {
2171        SMLoc Loc = getTok().getLoc();
2172        const MCExpr *Value;
2173        if (getParser().ParseExpression(Value))
2174          return true;
2175        // The expression must be the constant 0 or 1.
2176        if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2177          int Value = MCE->getValue();
2178          if (Value == 0)
2179            Flags &= ~DWARF2_FLAG_IS_STMT;
2180          else if (Value == 1)
2181            Flags |= DWARF2_FLAG_IS_STMT;
2182          else
2183            return Error(Loc, "is_stmt value not 0 or 1");
2184        }
2185        else {
2186          return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2187        }
2188      }
2189      else if (Name == "isa") {
2190        SMLoc Loc = getTok().getLoc();
2191        const MCExpr *Value;
2192        if (getParser().ParseExpression(Value))
2193          return true;
2194        // The expression must be a constant greater or equal to 0.
2195        if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2196          int Value = MCE->getValue();
2197          if (Value < 0)
2198            return Error(Loc, "isa number less than zero");
2199          Isa = Value;
2200        }
2201        else {
2202          return Error(Loc, "isa number not a constant value");
2203        }
2204      }
2205      else if (Name == "discriminator") {
2206        if (getParser().ParseAbsoluteExpression(Discriminator))
2207          return true;
2208      }
2209      else {
2210        return Error(Loc, "unknown sub-directive in '.loc' directive");
2211      }
2212
2213      if (getLexer().is(AsmToken::EndOfStatement))
2214        break;
2215    }
2216  }
2217
2218  getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2219                                      Isa, Discriminator);
2220
2221  return false;
2222}
2223
2224/// ParseDirectiveStabs
2225/// ::= .stabs string, number, number, number
2226bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2227                                           SMLoc DirectiveLoc) {
2228  return TokError("unsupported directive '" + Directive + "'");
2229}
2230
2231/// ParseDirectiveCFIStartProc
2232/// ::= .cfi_startproc
2233bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2234                                                  SMLoc DirectiveLoc) {
2235  return getStreamer().EmitCFIStartProc();
2236}
2237
2238/// ParseDirectiveCFIEndProc
2239/// ::= .cfi_endproc
2240bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
2241  return getStreamer().EmitCFIEndProc();
2242}
2243
2244/// ParseRegisterOrRegisterNumber - parse register name or number.
2245bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2246                                                     SMLoc DirectiveLoc) {
2247  unsigned RegNo;
2248
2249  if (getLexer().is(AsmToken::Percent)) {
2250    if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2251      DirectiveLoc))
2252      return true;
2253    Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2254  } else
2255    return getParser().ParseAbsoluteExpression(Register);
2256
2257  return false;
2258}
2259
2260/// ParseDirectiveCFIDefCfa
2261/// ::= .cfi_def_cfa register,  offset
2262bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2263                                               SMLoc DirectiveLoc) {
2264  int64_t Register = 0;
2265  if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2266    return true;
2267
2268  if (getLexer().isNot(AsmToken::Comma))
2269    return TokError("unexpected token in directive");
2270  Lex();
2271
2272  int64_t Offset = 0;
2273  if (getParser().ParseAbsoluteExpression(Offset))
2274    return true;
2275
2276  return getStreamer().EmitCFIDefCfa(Register, Offset);
2277}
2278
2279/// ParseDirectiveCFIDefCfaOffset
2280/// ::= .cfi_def_cfa_offset offset
2281bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2282                                                     SMLoc DirectiveLoc) {
2283  int64_t Offset = 0;
2284  if (getParser().ParseAbsoluteExpression(Offset))
2285    return true;
2286
2287  return getStreamer().EmitCFIDefCfaOffset(Offset);
2288}
2289
2290/// ParseDirectiveCFIDefCfaRegister
2291/// ::= .cfi_def_cfa_register register
2292bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2293                                                       SMLoc DirectiveLoc) {
2294  int64_t Register = 0;
2295  if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2296    return true;
2297
2298  return getStreamer().EmitCFIDefCfaRegister(Register);
2299}
2300
2301/// ParseDirectiveCFIOffset
2302/// ::= .cfi_off register, offset
2303bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2304  int64_t Register = 0;
2305  int64_t Offset = 0;
2306
2307  if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2308    return true;
2309
2310  if (getLexer().isNot(AsmToken::Comma))
2311    return TokError("unexpected token in directive");
2312  Lex();
2313
2314  if (getParser().ParseAbsoluteExpression(Offset))
2315    return true;
2316
2317  return getStreamer().EmitCFIOffset(Register, Offset);
2318}
2319
2320static bool isValidEncoding(int64_t Encoding) {
2321  if (Encoding & ~0xff)
2322    return false;
2323
2324  if (Encoding == dwarf::DW_EH_PE_omit)
2325    return true;
2326
2327  const unsigned Format = Encoding & 0xf;
2328  if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2329      Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2330      Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2331      Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2332    return false;
2333
2334  const unsigned Application = Encoding & 0x70;
2335  if (Application != dwarf::DW_EH_PE_absptr &&
2336      Application != dwarf::DW_EH_PE_pcrel)
2337    return false;
2338
2339  return true;
2340}
2341
2342/// ParseDirectiveCFIPersonalityOrLsda
2343/// ::= .cfi_personality encoding, [symbol_name]
2344/// ::= .cfi_lsda encoding, [symbol_name]
2345bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
2346                                                    SMLoc DirectiveLoc) {
2347  int64_t Encoding = 0;
2348  if (getParser().ParseAbsoluteExpression(Encoding))
2349    return true;
2350  if (Encoding == dwarf::DW_EH_PE_omit)
2351    return false;
2352
2353  if (!isValidEncoding(Encoding))
2354    return TokError("unsupported encoding.");
2355
2356  if (getLexer().isNot(AsmToken::Comma))
2357    return TokError("unexpected token in directive");
2358  Lex();
2359
2360  StringRef Name;
2361  if (getParser().ParseIdentifier(Name))
2362    return TokError("expected identifier in directive");
2363
2364  MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2365
2366  if (IDVal == ".cfi_personality")
2367    return getStreamer().EmitCFIPersonality(Sym, Encoding);
2368  else {
2369    assert(IDVal == ".cfi_lsda");
2370    return getStreamer().EmitCFILsda(Sym, Encoding);
2371  }
2372}
2373
2374/// ParseDirectiveCFIRememberState
2375/// ::= .cfi_remember_state
2376bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2377                                                      SMLoc DirectiveLoc) {
2378  return getStreamer().EmitCFIRememberState();
2379}
2380
2381/// ParseDirectiveCFIRestoreState
2382/// ::= .cfi_remember_state
2383bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2384                                                     SMLoc DirectiveLoc) {
2385  return getStreamer().EmitCFIRestoreState();
2386}
2387
2388/// ParseDirectiveMacrosOnOff
2389/// ::= .macros_on
2390/// ::= .macros_off
2391bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2392                                                 SMLoc DirectiveLoc) {
2393  if (getLexer().isNot(AsmToken::EndOfStatement))
2394    return Error(getLexer().getLoc(),
2395                 "unexpected token in '" + Directive + "' directive");
2396
2397  getParser().MacrosEnabled = Directive == ".macros_on";
2398
2399  return false;
2400}
2401
2402/// ParseDirectiveMacro
2403/// ::= .macro name
2404bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2405                                           SMLoc DirectiveLoc) {
2406  StringRef Name;
2407  if (getParser().ParseIdentifier(Name))
2408    return TokError("expected identifier in directive");
2409
2410  if (getLexer().isNot(AsmToken::EndOfStatement))
2411    return TokError("unexpected token in '.macro' directive");
2412
2413  // Eat the end of statement.
2414  Lex();
2415
2416  AsmToken EndToken, StartToken = getTok();
2417
2418  // Lex the macro definition.
2419  for (;;) {
2420    // Check whether we have reached the end of the file.
2421    if (getLexer().is(AsmToken::Eof))
2422      return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2423
2424    // Otherwise, check whether we have reach the .endmacro.
2425    if (getLexer().is(AsmToken::Identifier) &&
2426        (getTok().getIdentifier() == ".endm" ||
2427         getTok().getIdentifier() == ".endmacro")) {
2428      EndToken = getTok();
2429      Lex();
2430      if (getLexer().isNot(AsmToken::EndOfStatement))
2431        return TokError("unexpected token in '" + EndToken.getIdentifier() +
2432                        "' directive");
2433      break;
2434    }
2435
2436    // Otherwise, scan til the end of the statement.
2437    getParser().EatToEndOfStatement();
2438  }
2439
2440  if (getParser().MacroMap.lookup(Name)) {
2441    return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2442  }
2443
2444  const char *BodyStart = StartToken.getLoc().getPointer();
2445  const char *BodyEnd = EndToken.getLoc().getPointer();
2446  StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2447  getParser().MacroMap[Name] = new Macro(Name, Body);
2448  return false;
2449}
2450
2451/// ParseDirectiveEndMacro
2452/// ::= .endm
2453/// ::= .endmacro
2454bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2455                                           SMLoc DirectiveLoc) {
2456  if (getLexer().isNot(AsmToken::EndOfStatement))
2457    return TokError("unexpected token in '" + Directive + "' directive");
2458
2459  // If we are inside a macro instantiation, terminate the current
2460  // instantiation.
2461  if (!getParser().ActiveMacros.empty()) {
2462    getParser().HandleMacroExit();
2463    return false;
2464  }
2465
2466  // Otherwise, this .endmacro is a stray entry in the file; well formed
2467  // .endmacro directives are handled during the macro definition parsing.
2468  return TokError("unexpected '" + Directive + "' in file, "
2469                  "no current macro definition");
2470}
2471
2472bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
2473  getParser().CheckForValidSection();
2474
2475  const MCExpr *Value;
2476
2477  if (getParser().ParseExpression(Value))
2478    return true;
2479
2480  if (getLexer().isNot(AsmToken::EndOfStatement))
2481    return TokError("unexpected token in directive");
2482
2483  if (DirName[1] == 's')
2484    getStreamer().EmitSLEB128Value(Value);
2485  else
2486    getStreamer().EmitULEB128Value(Value);
2487
2488  return false;
2489}
2490
2491
2492/// \brief Create an MCAsmParser instance.
2493MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2494                                     MCContext &C, MCStreamer &Out,
2495                                     const MCAsmInfo &MAI) {
2496  return new AsmParser(T, SM, C, Out, MAI);
2497}
2498