1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements the parser for assembly files.
12 //===----------------------------------------------------------------------===//
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/MCDwarf.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCParser/AsmCond.h"
24 #include "llvm/MC/MCParser/AsmLexer.h"
25 #include "llvm/MC/MCParser/MCAsmParser.h"
26 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCSectionMachO.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCTargetAsmParser.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/SourceMgr.h"
36 #include "llvm/Support/raw_ostream.h"
42 FatalAssemblerWarnings("fatal-assembler-warnings",
43 cl::desc("Consider warnings as error"));
47 /// \brief Helper class for tracking macro definitions.
51 std::vector<StringRef> Parameters;
54 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
55 Name(N), Body(B), Parameters(P) {}
58 /// \brief Helper class for storing information about an active macro
60 struct MacroInstantiation {
61 /// The macro being instantiated.
62 const Macro *TheMacro;
64 /// The macro instantiation with substitutions.
65 MemoryBuffer *Instantiation;
67 /// The location of the instantiation.
68 SMLoc InstantiationLoc;
70 /// The location where parsing should resume upon instantiation completion.
74 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
78 /// \brief The concrete assembly parser instance.
79 class AsmParser : public MCAsmParser {
80 friend class GenericAsmParser;
82 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
83 void operator=(const AsmParser &); // DO NOT IMPLEMENT
90 SourceMgr::DiagHandlerTy SavedDiagHandler;
91 void *SavedDiagContext;
92 MCAsmParserExtension *GenericParser;
93 MCAsmParserExtension *PlatformParser;
95 /// This is the current buffer index we're lexing from as managed by the
100 std::vector<AsmCond> TheCondStack;
102 /// DirectiveMap - This is a table handlers for directives. Each handler is
103 /// invoked after the directive identifier is read and is responsible for
104 /// parsing and validating the rest of the directive. The handler is passed
105 /// in the directive name and the location of the directive keyword.
106 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
108 /// MacroMap - Map of currently defined macros.
109 StringMap<Macro*> MacroMap;
111 /// ActiveMacros - Stack of active macro instantiations.
112 std::vector<MacroInstantiation*> ActiveMacros;
114 /// Boolean tracking whether macro substitution is enabled.
115 unsigned MacrosEnabled : 1;
117 /// Flag tracking whether any errors have been encountered.
118 unsigned HadError : 1;
120 /// The values from the last parsed cpp hash file line comment if any.
121 StringRef CppHashFilename;
122 int64_t CppHashLineNumber;
126 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
127 const MCAsmInfo &MAI);
130 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
132 void AddDirectiveHandler(MCAsmParserExtension *Object,
134 DirectiveHandler Handler) {
135 DirectiveMap[Directive] = std::make_pair(Object, Handler);
139 /// @name MCAsmParser Interface
142 virtual SourceMgr &getSourceManager() { return SrcMgr; }
143 virtual MCAsmLexer &getLexer() { return Lexer; }
144 virtual MCContext &getContext() { return Ctx; }
145 virtual MCStreamer &getStreamer() { return Out; }
147 virtual bool Warning(SMLoc L, const Twine &Msg,
148 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
149 virtual bool Error(SMLoc L, const Twine &Msg,
150 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
152 const AsmToken &Lex();
154 bool ParseExpression(const MCExpr *&Res);
155 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
156 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
157 virtual bool ParseAbsoluteExpression(int64_t &Res);
162 void CheckForValidSection();
164 bool ParseStatement();
165 void EatToEndOfLine();
166 bool ParseCppHashLineFilenameComment(const SMLoc &L);
168 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
169 bool expandMacro(SmallString<256> &Buf, StringRef Body,
170 const std::vector<StringRef> &Parameters,
171 const std::vector<std::vector<AsmToken> > &A,
173 void HandleMacroExit();
175 void PrintMacroInstantiations();
176 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
177 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
178 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
180 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
182 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
183 bool EnterIncludeFile(const std::string &Filename);
185 /// \brief Reset the current lexer position to that given by \arg Loc. The
186 /// current token is not set; clients should ensure Lex() is called
188 void JumpToLoc(SMLoc Loc);
190 void EatToEndOfStatement();
192 /// \brief Parse up to the end of statement and a return the contents from the
193 /// current token until the end of the statement; the current token on exit
194 /// will be either the EndOfStatement or EOF.
195 StringRef ParseStringToEndOfStatement();
197 bool ParseAssignment(StringRef Name, bool allow_redef);
199 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
200 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
201 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
202 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
204 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
205 /// and set \arg Res to the identifier contents.
206 bool ParseIdentifier(StringRef &Res);
208 // Directive Parsing.
210 // ".ascii", ".asciiz", ".string"
211 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
212 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
213 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
214 bool ParseDirectiveFill(); // ".fill"
215 bool ParseDirectiveSpace(); // ".space"
216 bool ParseDirectiveZero(); // ".zero"
217 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
218 bool ParseDirectiveOrg(); // ".org"
219 // ".align{,32}", ".p2align{,w,l}"
220 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
222 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
223 /// accepts a single symbol (which should be a label or an external).
224 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
226 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
228 bool ParseDirectiveAbort(); // ".abort"
229 bool ParseDirectiveInclude(); // ".include"
231 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
232 // ".ifdef" or ".ifndef", depending on expect_defined
233 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
234 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
235 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
236 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
238 /// ParseEscapedString - Parse the current token as a string which may include
239 /// escaped characters and return the string contents.
240 bool ParseEscapedString(std::string &Data);
242 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
243 MCSymbolRefExpr::VariantKind Variant);
246 /// \brief Generic implementations of directive handling, etc. which is shared
247 /// (or the default, at least) for all assembler parser.
248 class GenericAsmParser : public MCAsmParserExtension {
249 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
250 void AddDirectiveHandler(StringRef Directive) {
251 getParser().AddDirectiveHandler(this, Directive,
252 HandleDirective<GenericAsmParser, Handler>);
255 GenericAsmParser() {}
257 AsmParser &getParser() {
258 return (AsmParser&) this->MCAsmParserExtension::getParser();
261 virtual void Initialize(MCAsmParser &Parser) {
262 // Call the base implementation.
263 this->MCAsmParserExtension::Initialize(Parser);
265 // Debugging directives.
266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
269 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
278 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
280 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
281 ".cfi_def_cfa_offset");
282 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
283 ".cfi_adjust_cfa_offset");
284 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
285 ".cfi_def_cfa_register");
286 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
288 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
291 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
293 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
295 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
297 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
299 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
302 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
304 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
306 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
307 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
308 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
310 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
311 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
314 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
316 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
317 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
318 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
319 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
320 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
321 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
322 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
323 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
324 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
325 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
326 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
327 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
328 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
329 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
330 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
331 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
332 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
334 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
335 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
336 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
338 bool ParseDirectiveLEB128(StringRef, SMLoc);
345 extern MCAsmParserExtension *createDarwinAsmParser();
346 extern MCAsmParserExtension *createELFAsmParser();
347 extern MCAsmParserExtension *createCOFFAsmParser();
351 enum { DEFAULT_ADDRSPACE = 0 };
353 AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
354 MCStreamer &_Out, const MCAsmInfo &_MAI)
355 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
356 GenericParser(new GenericAsmParser), PlatformParser(0),
357 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0) {
358 // Save the old handler.
359 SavedDiagHandler = SrcMgr.getDiagHandler();
360 SavedDiagContext = SrcMgr.getDiagContext();
361 // Set our own handler which calls the saved handler.
362 SrcMgr.setDiagHandler(DiagHandler, this);
363 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
365 // Initialize the generic parser.
366 GenericParser->Initialize(*this);
368 // Initialize the platform / file format parser.
370 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
372 if (_MAI.hasMicrosoftFastStdCallMangling()) {
373 PlatformParser = createCOFFAsmParser();
374 PlatformParser->Initialize(*this);
375 } else if (_MAI.hasSubsectionsViaSymbols()) {
376 PlatformParser = createDarwinAsmParser();
377 PlatformParser->Initialize(*this);
379 PlatformParser = createELFAsmParser();
380 PlatformParser->Initialize(*this);
384 AsmParser::~AsmParser() {
385 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
387 // Destroy any macros.
388 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
389 ie = MacroMap.end(); it != ie; ++it)
390 delete it->getValue();
392 delete PlatformParser;
393 delete GenericParser;
396 void AsmParser::PrintMacroInstantiations() {
397 // Print the active macro instantiation stack.
398 for (std::vector<MacroInstantiation*>::const_reverse_iterator
399 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
400 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
401 "while in macro instantiation");
404 bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
405 if (FatalAssemblerWarnings)
406 return Error(L, Msg, Ranges);
407 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
408 PrintMacroInstantiations();
412 bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
414 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
415 PrintMacroInstantiations();
419 bool AsmParser::EnterIncludeFile(const std::string &Filename) {
420 std::string IncludedFile;
421 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
427 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
432 void AsmParser::JumpToLoc(SMLoc Loc) {
433 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
434 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
437 const AsmToken &AsmParser::Lex() {
438 const AsmToken *tok = &Lexer.Lex();
440 if (tok->is(AsmToken::Eof)) {
441 // If this is the end of an included file, pop the parent file off the
443 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
444 if (ParentIncludeLoc != SMLoc()) {
445 JumpToLoc(ParentIncludeLoc);
450 if (tok->is(AsmToken::Error))
451 Error(Lexer.getErrLoc(), Lexer.getErr());
456 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
457 // Create the initial section, if requested.
458 if (!NoInitialTextSection)
465 AsmCond StartingCondState = TheCondState;
467 // While we have input, parse each statement.
468 while (Lexer.isNot(AsmToken::Eof)) {
469 if (!ParseStatement()) continue;
471 // We had an error, validate that one was emitted and recover by skipping to
473 assert(HadError && "Parse statement returned an error, but none emitted!");
474 EatToEndOfStatement();
477 if (TheCondState.TheCond != StartingCondState.TheCond ||
478 TheCondState.Ignore != StartingCondState.Ignore)
479 return TokError("unmatched .ifs or .elses");
481 // Check to see there are no empty DwarfFile slots.
482 const std::vector<MCDwarfFile *> &MCDwarfFiles =
483 getContext().getMCDwarfFiles();
484 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
485 if (!MCDwarfFiles[i])
486 TokError("unassigned file number: " + Twine(i) + " for .file directives");
489 // Check to see that all assembler local symbols were actually defined.
490 // Targets that don't do subsections via symbols may not want this, though,
491 // so conservatively exclude them. Only do this if we're finalizing, though,
492 // as otherwise we won't necessarilly have seen everything yet.
493 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
494 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
495 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
498 MCSymbol *Sym = i->getValue();
499 // Variable symbols may not be marked as defined, so check those
500 // explicitly. If we know it's a variable, we have a definition for
501 // the purposes of this check.
502 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
503 // FIXME: We would really like to refer back to where the symbol was
504 // first referenced for a source location. We need to add something
505 // to track that. Currently, we just point to the end of the file.
506 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
507 "assembler local symbol '" + Sym->getName() +
513 // Finalize the output stream if there are no errors and if the client wants
515 if (!HadError && !NoFinalize)
521 void AsmParser::CheckForValidSection() {
522 if (!getStreamer().getCurrentSection()) {
523 TokError("expected section directive before assembly directive");
524 Out.SwitchSection(Ctx.getMachOSection(
526 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
527 0, SectionKind::getText()));
531 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
532 void AsmParser::EatToEndOfStatement() {
533 while (Lexer.isNot(AsmToken::EndOfStatement) &&
534 Lexer.isNot(AsmToken::Eof))
538 if (Lexer.is(AsmToken::EndOfStatement))
542 StringRef AsmParser::ParseStringToEndOfStatement() {
543 const char *Start = getTok().getLoc().getPointer();
545 while (Lexer.isNot(AsmToken::EndOfStatement) &&
546 Lexer.isNot(AsmToken::Eof))
549 const char *End = getTok().getLoc().getPointer();
550 return StringRef(Start, End - Start);
553 /// ParseParenExpr - Parse a paren expression and return it.
554 /// NOTE: This assumes the leading '(' has already been consumed.
556 /// parenexpr ::= expr)
558 bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
559 if (ParseExpression(Res)) return true;
560 if (Lexer.isNot(AsmToken::RParen))
561 return TokError("expected ')' in parentheses expression");
562 EndLoc = Lexer.getLoc();
567 /// ParseBracketExpr - Parse a bracket expression and return it.
568 /// NOTE: This assumes the leading '[' has already been consumed.
570 /// bracketexpr ::= expr]
572 bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
573 if (ParseExpression(Res)) return true;
574 if (Lexer.isNot(AsmToken::RBrac))
575 return TokError("expected ']' in brackets expression");
576 EndLoc = Lexer.getLoc();
581 /// ParsePrimaryExpr - Parse a primary expression and return it.
582 /// primaryexpr ::= (parenexpr
583 /// primaryexpr ::= symbol
584 /// primaryexpr ::= number
585 /// primaryexpr ::= '.'
586 /// primaryexpr ::= ~,+,- primaryexpr
587 bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
588 switch (Lexer.getKind()) {
590 return TokError("unknown token in expression");
591 // If we have an error assume that we've already handled it.
592 case AsmToken::Error:
594 case AsmToken::Exclaim:
595 Lex(); // Eat the operator.
596 if (ParsePrimaryExpr(Res, EndLoc))
598 Res = MCUnaryExpr::CreateLNot(Res, getContext());
600 case AsmToken::Dollar:
601 case AsmToken::String:
602 case AsmToken::Identifier: {
603 EndLoc = Lexer.getLoc();
605 StringRef Identifier;
606 if (ParseIdentifier(Identifier))
609 // This is a symbol reference.
610 std::pair<StringRef, StringRef> Split = Identifier.split('@');
611 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
613 // Lookup the symbol variant if used.
614 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
615 if (Split.first.size() != Identifier.size()) {
616 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
617 if (Variant == MCSymbolRefExpr::VK_Invalid) {
618 Variant = MCSymbolRefExpr::VK_None;
619 return TokError("invalid variant '" + Split.second + "'");
623 // If this is an absolute variable reference, substitute it now to preserve
624 // semantics in the face of reassignment.
625 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
627 return Error(EndLoc, "unexpected modifier on variable reference");
629 Res = Sym->getVariableValue();
633 // Otherwise create a symbol ref.
634 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
637 case AsmToken::Integer: {
638 SMLoc Loc = getTok().getLoc();
639 int64_t IntVal = getTok().getIntVal();
640 Res = MCConstantExpr::Create(IntVal, getContext());
641 EndLoc = Lexer.getLoc();
643 // Look for 'b' or 'f' following an Integer as a directional label
644 if (Lexer.getKind() == AsmToken::Identifier) {
645 StringRef IDVal = getTok().getString();
646 if (IDVal == "f" || IDVal == "b"){
647 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
648 IDVal == "f" ? 1 : 0);
649 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
651 if(IDVal == "b" && Sym->isUndefined())
652 return Error(Loc, "invalid reference to undefined symbol");
653 EndLoc = Lexer.getLoc();
654 Lex(); // Eat identifier.
659 case AsmToken::Real: {
660 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
661 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
662 Res = MCConstantExpr::Create(IntVal, getContext());
666 case AsmToken::Dot: {
667 // This is a '.' reference, which references the current PC. Emit a
668 // temporary label to the streamer and refer to it.
669 MCSymbol *Sym = Ctx.CreateTempSymbol();
671 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
672 EndLoc = Lexer.getLoc();
673 Lex(); // Eat identifier.
676 case AsmToken::LParen:
677 Lex(); // Eat the '('.
678 return ParseParenExpr(Res, EndLoc);
679 case AsmToken::LBrac:
680 if (!PlatformParser->HasBracketExpressions())
681 return TokError("brackets expression not supported on this target");
682 Lex(); // Eat the '['.
683 return ParseBracketExpr(Res, EndLoc);
684 case AsmToken::Minus:
685 Lex(); // Eat the operator.
686 if (ParsePrimaryExpr(Res, EndLoc))
688 Res = MCUnaryExpr::CreateMinus(Res, getContext());
691 Lex(); // Eat the operator.
692 if (ParsePrimaryExpr(Res, EndLoc))
694 Res = MCUnaryExpr::CreatePlus(Res, getContext());
696 case AsmToken::Tilde:
697 Lex(); // Eat the operator.
698 if (ParsePrimaryExpr(Res, EndLoc))
700 Res = MCUnaryExpr::CreateNot(Res, getContext());
705 bool AsmParser::ParseExpression(const MCExpr *&Res) {
707 return ParseExpression(Res, EndLoc);
711 AsmParser::ApplyModifierToExpr(const MCExpr *E,
712 MCSymbolRefExpr::VariantKind Variant) {
713 // Recurse over the given expression, rebuilding it to apply the given variant
714 // if there is exactly one symbol.
715 switch (E->getKind()) {
717 case MCExpr::Constant:
720 case MCExpr::SymbolRef: {
721 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
723 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
724 TokError("invalid variant on expression '" +
725 getTok().getIdentifier() + "' (already modified)");
729 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
732 case MCExpr::Unary: {
733 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
734 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
737 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
740 case MCExpr::Binary: {
741 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
742 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
743 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
748 if (!LHS) LHS = BE->getLHS();
749 if (!RHS) RHS = BE->getRHS();
751 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
755 assert(0 && "Invalid expression kind!");
759 /// ParseExpression - Parse an expression and return it.
761 /// expr ::= expr &&,|| expr -> lowest.
762 /// expr ::= expr |,^,&,! expr
763 /// expr ::= expr ==,!=,<>,<,<=,>,>= expr
764 /// expr ::= expr <<,>> expr
765 /// expr ::= expr +,- expr
766 /// expr ::= expr *,/,% expr -> highest.
767 /// expr ::= primaryexpr
769 bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
770 // Parse the expression.
772 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
775 // As a special case, we support 'a op b @ modifier' by rewriting the
776 // expression to include the modifier. This is inefficient, but in general we
777 // expect users to use 'a@modifier op b'.
778 if (Lexer.getKind() == AsmToken::At) {
781 if (Lexer.isNot(AsmToken::Identifier))
782 return TokError("unexpected symbol modifier following '@'");
784 MCSymbolRefExpr::VariantKind Variant =
785 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
786 if (Variant == MCSymbolRefExpr::VK_Invalid)
787 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
789 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
791 return TokError("invalid modifier '" + getTok().getIdentifier() +
792 "' (no symbols present)");
800 // Try to constant fold it up front, if possible.
802 if (Res->EvaluateAsAbsolute(Value))
803 Res = MCConstantExpr::Create(Value, getContext());
808 bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
810 return ParseParenExpr(Res, EndLoc) ||
811 ParseBinOpRHS(1, Res, EndLoc);
814 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
817 SMLoc StartLoc = Lexer.getLoc();
818 if (ParseExpression(Expr))
821 if (!Expr->EvaluateAsAbsolute(Res))
822 return Error(StartLoc, "expected absolute expression");
827 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
828 MCBinaryExpr::Opcode &Kind) {
831 return 0; // not a binop.
833 // Lowest Precedence: &&, ||
834 case AsmToken::AmpAmp:
835 Kind = MCBinaryExpr::LAnd;
837 case AsmToken::PipePipe:
838 Kind = MCBinaryExpr::LOr;
842 // Low Precedence: |, &, ^
844 // FIXME: gas seems to support '!' as an infix operator?
846 Kind = MCBinaryExpr::Or;
848 case AsmToken::Caret:
849 Kind = MCBinaryExpr::Xor;
852 Kind = MCBinaryExpr::And;
855 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
856 case AsmToken::EqualEqual:
857 Kind = MCBinaryExpr::EQ;
859 case AsmToken::ExclaimEqual:
860 case AsmToken::LessGreater:
861 Kind = MCBinaryExpr::NE;
864 Kind = MCBinaryExpr::LT;
866 case AsmToken::LessEqual:
867 Kind = MCBinaryExpr::LTE;
869 case AsmToken::Greater:
870 Kind = MCBinaryExpr::GT;
872 case AsmToken::GreaterEqual:
873 Kind = MCBinaryExpr::GTE;
876 // Intermediate Precedence: <<, >>
877 case AsmToken::LessLess:
878 Kind = MCBinaryExpr::Shl;
880 case AsmToken::GreaterGreater:
881 Kind = MCBinaryExpr::Shr;
884 // High Intermediate Precedence: +, -
886 Kind = MCBinaryExpr::Add;
888 case AsmToken::Minus:
889 Kind = MCBinaryExpr::Sub;
892 // Highest Precedence: *, /, %
894 Kind = MCBinaryExpr::Mul;
896 case AsmToken::Slash:
897 Kind = MCBinaryExpr::Div;
899 case AsmToken::Percent:
900 Kind = MCBinaryExpr::Mod;
906 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
907 /// Res contains the LHS of the expression on input.
908 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
911 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
912 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
914 // If the next token is lower precedence than we are allowed to eat, return
915 // successfully with what we ate already.
916 if (TokPrec < Precedence)
921 // Eat the next primary expression.
923 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
925 // If BinOp binds less tightly with RHS than the operator after RHS, let
926 // the pending operator take RHS as its LHS.
927 MCBinaryExpr::Opcode Dummy;
928 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
929 if (TokPrec < NextTokPrec) {
930 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
933 // Merge LHS and RHS according to operator.
934 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
942 /// ::= EndOfStatement
943 /// ::= Label* Directive ...Operands... EndOfStatement
944 /// ::= Label* Identifier OperandList* EndOfStatement
945 bool AsmParser::ParseStatement() {
946 if (Lexer.is(AsmToken::EndOfStatement)) {
952 // Statements always start with an identifier or are a full line comment.
953 AsmToken ID = getTok();
954 SMLoc IDLoc = ID.getLoc();
956 int64_t LocalLabelVal = -1;
957 // A full line comment is a '#' as the first token.
958 if (Lexer.is(AsmToken::Hash))
959 return ParseCppHashLineFilenameComment(IDLoc);
961 // Allow an integer followed by a ':' as a directional local label.
962 if (Lexer.is(AsmToken::Integer)) {
963 LocalLabelVal = getTok().getIntVal();
964 if (LocalLabelVal < 0) {
965 if (!TheCondState.Ignore)
966 return TokError("unexpected token at start of statement");
970 IDVal = getTok().getString();
971 Lex(); // Consume the integer token to be used as an identifier token.
972 if (Lexer.getKind() != AsmToken::Colon) {
973 if (!TheCondState.Ignore)
974 return TokError("unexpected token at start of statement");
978 } else if (Lexer.is(AsmToken::Dot)) {
979 // Treat '.' as a valid identifier in this context.
983 } else if (ParseIdentifier(IDVal)) {
984 if (!TheCondState.Ignore)
985 return TokError("unexpected token at start of statement");
990 // Handle conditional assembly here before checking for skipping. We
991 // have to do this so that .endif isn't skipped in a ".if 0" block for
994 return ParseDirectiveIf(IDLoc);
995 if (IDVal == ".ifdef")
996 return ParseDirectiveIfdef(IDLoc, true);
997 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
998 return ParseDirectiveIfdef(IDLoc, false);
999 if (IDVal == ".elseif")
1000 return ParseDirectiveElseIf(IDLoc);
1001 if (IDVal == ".else")
1002 return ParseDirectiveElse(IDLoc);
1003 if (IDVal == ".endif")
1004 return ParseDirectiveEndIf(IDLoc);
1006 // If we are in a ".if 0" block, ignore this statement.
1007 if (TheCondState.Ignore) {
1008 EatToEndOfStatement();
1012 // FIXME: Recurse on local labels?
1014 // See what kind of statement we have.
1015 switch (Lexer.getKind()) {
1016 case AsmToken::Colon: {
1017 CheckForValidSection();
1019 // identifier ':' -> Label.
1022 // Diagnose attempt to use '.' as a label.
1024 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1026 // Diagnose attempt to use a variable as a label.
1028 // FIXME: Diagnostics. Note the location of the definition as a label.
1029 // FIXME: This doesn't diagnose assignment to a symbol which has been
1030 // implicitly marked as external.
1032 if (LocalLabelVal == -1)
1033 Sym = getContext().GetOrCreateSymbol(IDVal);
1035 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
1036 if (!Sym->isUndefined() || Sym->isVariable())
1037 return Error(IDLoc, "invalid symbol redefinition");
1042 // Consume any end of statement token, if present, to avoid spurious
1043 // AddBlankLine calls().
1044 if (Lexer.is(AsmToken::EndOfStatement)) {
1046 if (Lexer.is(AsmToken::Eof))
1050 return ParseStatement();
1053 case AsmToken::Equal:
1054 // identifier '=' ... -> assignment statement
1057 return ParseAssignment(IDVal, true);
1059 default: // Normal instruction or directive.
1063 // If macros are enabled, check to see if this is a macro instantiation.
1065 if (const Macro *M = MacroMap.lookup(IDVal))
1066 return HandleMacroEntry(IDVal, IDLoc, M);
1068 // Otherwise, we have a normal instruction or directive.
1069 if (IDVal[0] == '.' && IDVal != ".") {
1070 // Assembler features
1071 if (IDVal == ".set" || IDVal == ".equ")
1072 return ParseDirectiveSet(IDVal, true);
1073 if (IDVal == ".equiv")
1074 return ParseDirectiveSet(IDVal, false);
1078 if (IDVal == ".ascii")
1079 return ParseDirectiveAscii(IDVal, false);
1080 if (IDVal == ".asciz" || IDVal == ".string")
1081 return ParseDirectiveAscii(IDVal, true);
1083 if (IDVal == ".byte")
1084 return ParseDirectiveValue(1);
1085 if (IDVal == ".short")
1086 return ParseDirectiveValue(2);
1087 if (IDVal == ".value")
1088 return ParseDirectiveValue(2);
1089 if (IDVal == ".2byte")
1090 return ParseDirectiveValue(2);
1091 if (IDVal == ".long")
1092 return ParseDirectiveValue(4);
1093 if (IDVal == ".int")
1094 return ParseDirectiveValue(4);
1095 if (IDVal == ".4byte")
1096 return ParseDirectiveValue(4);
1097 if (IDVal == ".quad")
1098 return ParseDirectiveValue(8);
1099 if (IDVal == ".8byte")
1100 return ParseDirectiveValue(8);
1101 if (IDVal == ".single" || IDVal == ".float")
1102 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1103 if (IDVal == ".double")
1104 return ParseDirectiveRealValue(APFloat::IEEEdouble);
1106 if (IDVal == ".align") {
1107 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1108 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1110 if (IDVal == ".align32") {
1111 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1112 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1114 if (IDVal == ".balign")
1115 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1116 if (IDVal == ".balignw")
1117 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1118 if (IDVal == ".balignl")
1119 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1120 if (IDVal == ".p2align")
1121 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1122 if (IDVal == ".p2alignw")
1123 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1124 if (IDVal == ".p2alignl")
1125 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1127 if (IDVal == ".org")
1128 return ParseDirectiveOrg();
1130 if (IDVal == ".fill")
1131 return ParseDirectiveFill();
1132 if (IDVal == ".space" || IDVal == ".skip")
1133 return ParseDirectiveSpace();
1134 if (IDVal == ".zero")
1135 return ParseDirectiveZero();
1137 // Symbol attribute directives
1139 if (IDVal == ".globl" || IDVal == ".global")
1140 return ParseDirectiveSymbolAttribute(MCSA_Global);
1141 if (IDVal == ".indirect_symbol")
1142 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
1143 if (IDVal == ".lazy_reference")
1144 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
1145 if (IDVal == ".no_dead_strip")
1146 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1147 if (IDVal == ".symbol_resolver")
1148 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1149 if (IDVal == ".private_extern")
1150 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1151 if (IDVal == ".reference")
1152 return ParseDirectiveSymbolAttribute(MCSA_Reference);
1153 if (IDVal == ".weak_definition")
1154 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1155 if (IDVal == ".weak_reference")
1156 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
1157 if (IDVal == ".weak_def_can_be_hidden")
1158 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1160 if (IDVal == ".comm" || IDVal == ".common")
1161 return ParseDirectiveComm(/*IsLocal=*/false);
1162 if (IDVal == ".lcomm")
1163 return ParseDirectiveComm(/*IsLocal=*/true);
1165 if (IDVal == ".abort")
1166 return ParseDirectiveAbort();
1167 if (IDVal == ".include")
1168 return ParseDirectiveInclude();
1170 if (IDVal == ".code16")
1171 return TokError(Twine(IDVal) + " not supported yet");
1173 // Look up the handler in the handler table.
1174 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1175 DirectiveMap.lookup(IDVal);
1177 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1179 // Target hook for parsing target specific directives.
1180 if (!getTargetParser().ParseDirective(ID))
1183 bool retval = Warning(IDLoc, "ignoring directive for now");
1184 EatToEndOfStatement();
1188 CheckForValidSection();
1190 // Canonicalize the opcode to lower case.
1191 SmallString<128> Opcode;
1192 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1193 Opcode.push_back(tolower(IDVal[i]));
1195 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
1196 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
1199 // Dump the parsed representation, if requested.
1200 if (getShowParsedOperands()) {
1201 SmallString<256> Str;
1202 raw_svector_ostream OS(Str);
1203 OS << "parsed instruction: [";
1204 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1207 ParsedOperands[i]->print(OS);
1211 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
1214 // If parsing succeeded, match the instruction.
1216 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1219 // Free any parsed operands.
1220 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1221 delete ParsedOperands[i];
1223 // Don't skip the rest of the line, the instruction parser is responsible for
1228 /// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1229 /// since they may not be able to be tokenized to get to the end of line token.
1230 void AsmParser::EatToEndOfLine() {
1231 Lexer.LexUntilEndOfLine();
1236 /// ParseCppHashLineFilenameComment as this:
1237 /// ::= # number "filename"
1238 /// or just as a full line comment if it doesn't have a number and a string.
1239 bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1240 Lex(); // Eat the hash token.
1242 if (getLexer().isNot(AsmToken::Integer)) {
1243 // Consume the line since in cases it is not a well-formed line directive,
1244 // as if were simply a full line comment.
1249 int64_t LineNumber = getTok().getIntVal();
1252 if (getLexer().isNot(AsmToken::String)) {
1257 StringRef Filename = getTok().getString();
1258 // Get rid of the enclosing quotes.
1259 Filename = Filename.substr(1, Filename.size()-2);
1261 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1263 CppHashFilename = Filename;
1264 CppHashLineNumber = LineNumber;
1266 // Ignore any trailing characters, they're just comment.
1271 /// DiagHandler - will use the the last parsed cpp hash line filename comment
1272 /// for the Filename and LineNo if any in the diagnostic.
1273 void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1274 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1275 raw_ostream &OS = errs();
1277 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1278 const SMLoc &DiagLoc = Diag.getLoc();
1279 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1280 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1282 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1283 // before printing the message.
1284 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1285 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
1286 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1287 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1290 // If we have not parsed a cpp hash line filename comment or the source
1291 // manager changed or buffer changed (like in a nested include) then just
1292 // print the normal diagnostic using its Filename and LineNo.
1293 if (!Parser->CppHashLineNumber ||
1294 &DiagSrcMgr != &Parser->SrcMgr ||
1295 DiagBuf != CppHashBuf) {
1296 if (Parser->SavedDiagHandler)
1297 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1303 // Use the CppHashFilename and calculate a line number based on the
1304 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1306 const std::string Filename = Parser->CppHashFilename;
1308 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1309 int CppHashLocLineNo =
1310 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1311 int LineNo = Parser->CppHashLineNumber - 1 +
1312 (DiagLocLineNo - CppHashLocLineNo);
1314 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1315 Filename, LineNo, Diag.getColumnNo(),
1316 Diag.getKind(), Diag.getMessage(),
1317 Diag.getLineContents(), Diag.getRanges());
1319 if (Parser->SavedDiagHandler)
1320 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1322 NewDiag.print(0, OS);
1325 bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1326 const std::vector<StringRef> &Parameters,
1327 const std::vector<std::vector<AsmToken> > &A,
1329 raw_svector_ostream OS(Buf);
1330 unsigned NParameters = Parameters.size();
1331 if (NParameters != 0 && NParameters != A.size())
1332 return Error(L, "Wrong number of arguments");
1334 while (!Body.empty()) {
1335 // Scan for the next substitution.
1336 std::size_t End = Body.size(), Pos = 0;
1337 for (; Pos != End; ++Pos) {
1338 // Check for a substitution or escape.
1340 // This macro has no parameters, look for $0, $1, etc.
1341 if (Body[Pos] != '$' || Pos + 1 == End)
1344 char Next = Body[Pos + 1];
1345 if (Next == '$' || Next == 'n' || isdigit(Next))
1348 // This macro has parameters, look for \foo, \bar, etc.
1349 if (Body[Pos] == '\\' && Pos + 1 != End)
1355 OS << Body.slice(0, Pos);
1357 // Check if we reached the end.
1362 switch (Body[Pos+1]) {
1368 // $n => number of arguments
1373 // $[0-9] => argument
1375 // Missing arguments are ignored.
1376 unsigned Index = Body[Pos+1] - '0';
1377 if (Index >= A.size())
1380 // Otherwise substitute with the token values, with spaces eliminated.
1381 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1382 ie = A[Index].end(); it != ie; ++it)
1383 OS << it->getString();
1389 unsigned I = Pos + 1;
1390 while (isalnum(Body[I]) && I + 1 != End)
1393 const char *Begin = Body.data() + Pos +1;
1394 StringRef Argument(Begin, I - (Pos +1));
1396 for (; Index < NParameters; ++Index)
1397 if (Parameters[Index] == Argument)
1400 // FIXME: We should error at the macro definition.
1401 if (Index == NParameters)
1402 return Error(L, "Parameter not found");
1404 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1405 ie = A[Index].end(); it != ie; ++it)
1406 OS << it->getString();
1408 Pos += 1 + Argument.size();
1410 // Update the scan point.
1411 Body = Body.substr(Pos);
1414 // We include the .endmacro in the buffer as our queue to exit the macro
1416 OS << ".endmacro\n";
1420 MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1422 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1426 bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1428 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1429 // this, although we should protect against infinite loops.
1430 if (ActiveMacros.size() == 20)
1431 return TokError("macros cannot be nested more than 20 levels deep");
1433 // Parse the macro instantiation arguments.
1434 std::vector<std::vector<AsmToken> > MacroArguments;
1435 MacroArguments.push_back(std::vector<AsmToken>());
1436 unsigned ParenLevel = 0;
1438 if (Lexer.is(AsmToken::Eof))
1439 return TokError("unexpected token in macro instantiation");
1440 if (Lexer.is(AsmToken::EndOfStatement))
1443 // If we aren't inside parentheses and this is a comma, start a new token
1445 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1446 MacroArguments.push_back(std::vector<AsmToken>());
1448 // Adjust the current parentheses level.
1449 if (Lexer.is(AsmToken::LParen))
1451 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1454 // Append the token to the current argument list.
1455 MacroArguments.back().push_back(getTok());
1460 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1461 // to hold the macro body with substitutions.
1462 SmallString<256> Buf;
1463 StringRef Body = M->Body;
1465 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1468 MemoryBuffer *Instantiation =
1469 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1471 // Create the macro instantiation object and add to the current macro
1472 // instantiation stack.
1473 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
1476 ActiveMacros.push_back(MI);
1478 // Jump to the macro instantiation and prime the lexer.
1479 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1480 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1486 void AsmParser::HandleMacroExit() {
1487 // Jump to the EndOfStatement we should return to, and consume it.
1488 JumpToLoc(ActiveMacros.back()->ExitLoc);
1491 // Pop the instantiation entry.
1492 delete ActiveMacros.back();
1493 ActiveMacros.pop_back();
1496 static void MarkUsed(const MCExpr *Value) {
1497 switch (Value->getKind()) {
1498 case MCExpr::Binary:
1499 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1500 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1502 case MCExpr::Target:
1503 case MCExpr::Constant:
1505 case MCExpr::SymbolRef: {
1506 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1510 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1515 bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
1516 // FIXME: Use better location, we should use proper tokens.
1517 SMLoc EqualLoc = Lexer.getLoc();
1519 const MCExpr *Value;
1520 if (ParseExpression(Value))
1525 if (Lexer.isNot(AsmToken::EndOfStatement))
1526 return TokError("unexpected token in assignment");
1528 // Error on assignment to '.'.
1530 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1531 "(use '.space' or '.org').)"));
1534 // Eat the end of statement marker.
1537 // Validate that the LHS is allowed to be a variable (either it has not been
1538 // used as a symbol, or it is an absolute symbol).
1539 MCSymbol *Sym = getContext().LookupSymbol(Name);
1541 // Diagnose assignment to a label.
1543 // FIXME: Diagnostics. Note the location of the definition as a label.
1544 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
1545 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
1546 ; // Allow redefinitions of undefined symbols only used in directives.
1547 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
1548 return Error(EqualLoc, "redefinition of '" + Name + "'");
1549 else if (!Sym->isVariable())
1550 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
1551 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
1552 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1555 // Don't count these checks as uses.
1556 Sym->setUsed(false);
1558 Sym = getContext().GetOrCreateSymbol(Name);
1560 // FIXME: Handle '.'.
1562 // Do the assignment.
1563 Out.EmitAssignment(Sym, Value);
1568 /// ParseIdentifier:
1571 bool AsmParser::ParseIdentifier(StringRef &Res) {
1572 // The assembler has relaxed rules for accepting identifiers, in particular we
1573 // allow things like '.globl $foo', which would normally be separate
1574 // tokens. At this level, we have already lexed so we cannot (currently)
1575 // handle this as a context dependent token, instead we detect adjacent tokens
1576 // and return the combined identifier.
1577 if (Lexer.is(AsmToken::Dollar)) {
1578 SMLoc DollarLoc = getLexer().getLoc();
1580 // Consume the dollar sign, and check for a following identifier.
1582 if (Lexer.isNot(AsmToken::Identifier))
1585 // We have a '$' followed by an identifier, make sure they are adjacent.
1586 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1589 // Construct the joined identifier and consume the token.
1590 Res = StringRef(DollarLoc.getPointer(),
1591 getTok().getIdentifier().size() + 1);
1596 if (Lexer.isNot(AsmToken::Identifier) &&
1597 Lexer.isNot(AsmToken::String))
1600 Res = getTok().getIdentifier();
1602 Lex(); // Consume the identifier token.
1607 /// ParseDirectiveSet:
1608 /// ::= .equ identifier ',' expression
1609 /// ::= .equiv identifier ',' expression
1610 /// ::= .set identifier ',' expression
1611 bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
1614 if (ParseIdentifier(Name))
1615 return TokError("expected identifier after '" + Twine(IDVal) + "'");
1617 if (getLexer().isNot(AsmToken::Comma))
1618 return TokError("unexpected token in '" + Twine(IDVal) + "'");
1621 return ParseAssignment(Name, allow_redef);
1624 bool AsmParser::ParseEscapedString(std::string &Data) {
1625 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
1628 StringRef Str = getTok().getStringContents();
1629 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1630 if (Str[i] != '\\') {
1635 // Recognize escaped characters. Note that this escape semantics currently
1636 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1639 return TokError("unexpected backslash at end of string");
1641 // Recognize octal sequences.
1642 if ((unsigned) (Str[i] - '0') <= 7) {
1643 // Consume up to three octal characters.
1644 unsigned Value = Str[i] - '0';
1646 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1648 Value = Value * 8 + (Str[i] - '0');
1650 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1652 Value = Value * 8 + (Str[i] - '0');
1657 return TokError("invalid octal escape sequence (out of range)");
1659 Data += (unsigned char) Value;
1663 // Otherwise recognize individual escapes.
1666 // Just reject invalid escape sequences for now.
1667 return TokError("invalid escape sequence (unrecognized character)");
1669 case 'b': Data += '\b'; break;
1670 case 'f': Data += '\f'; break;
1671 case 'n': Data += '\n'; break;
1672 case 'r': Data += '\r'; break;
1673 case 't': Data += '\t'; break;
1674 case '"': Data += '"'; break;
1675 case '\\': Data += '\\'; break;
1682 /// ParseDirectiveAscii:
1683 /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1684 bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
1685 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1686 CheckForValidSection();
1689 if (getLexer().isNot(AsmToken::String))
1690 return TokError("expected string in '" + Twine(IDVal) + "' directive");
1693 if (ParseEscapedString(Data))
1696 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
1698 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1702 if (getLexer().is(AsmToken::EndOfStatement))
1705 if (getLexer().isNot(AsmToken::Comma))
1706 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
1715 /// ParseDirectiveValue
1716 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1717 bool AsmParser::ParseDirectiveValue(unsigned Size) {
1718 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1719 CheckForValidSection();
1722 const MCExpr *Value;
1723 SMLoc ExprLoc = getLexer().getLoc();
1724 if (ParseExpression(Value))
1727 // Special case constant expressions to match code generator.
1728 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1729 assert(Size <= 8 && "Invalid size");
1730 uint64_t IntValue = MCE->getValue();
1731 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1732 return Error(ExprLoc, "literal value out of range for directive");
1733 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1735 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
1737 if (getLexer().is(AsmToken::EndOfStatement))
1740 // FIXME: Improve diagnostic.
1741 if (getLexer().isNot(AsmToken::Comma))
1742 return TokError("unexpected token in directive");
1751 /// ParseDirectiveRealValue
1752 /// ::= (.single | .double) [ expression (, expression)* ]
1753 bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1754 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1755 CheckForValidSection();
1758 // We don't truly support arithmetic on floating point expressions, so we
1759 // have to manually parse unary prefixes.
1761 if (getLexer().is(AsmToken::Minus)) {
1764 } else if (getLexer().is(AsmToken::Plus))
1767 if (getLexer().isNot(AsmToken::Integer) &&
1768 getLexer().isNot(AsmToken::Real) &&
1769 getLexer().isNot(AsmToken::Identifier))
1770 return TokError("unexpected token in directive");
1772 // Convert to an APFloat.
1773 APFloat Value(Semantics);
1774 StringRef IDVal = getTok().getString();
1775 if (getLexer().is(AsmToken::Identifier)) {
1776 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1777 Value = APFloat::getInf(Semantics);
1778 else if (!IDVal.compare_lower("nan"))
1779 Value = APFloat::getNaN(Semantics, false, ~0);
1781 return TokError("invalid floating point literal");
1782 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
1783 APFloat::opInvalidOp)
1784 return TokError("invalid floating point literal");
1788 // Consume the numeric token.
1791 // Emit the value as an integer.
1792 APInt AsInt = Value.bitcastToAPInt();
1793 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1794 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1796 if (getLexer().is(AsmToken::EndOfStatement))
1799 if (getLexer().isNot(AsmToken::Comma))
1800 return TokError("unexpected token in directive");
1809 /// ParseDirectiveSpace
1810 /// ::= .space expression [ , expression ]
1811 bool AsmParser::ParseDirectiveSpace() {
1812 CheckForValidSection();
1815 if (ParseAbsoluteExpression(NumBytes))
1818 int64_t FillExpr = 0;
1819 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1820 if (getLexer().isNot(AsmToken::Comma))
1821 return TokError("unexpected token in '.space' directive");
1824 if (ParseAbsoluteExpression(FillExpr))
1827 if (getLexer().isNot(AsmToken::EndOfStatement))
1828 return TokError("unexpected token in '.space' directive");
1834 return TokError("invalid number of bytes in '.space' directive");
1836 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1837 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
1842 /// ParseDirectiveZero
1843 /// ::= .zero expression
1844 bool AsmParser::ParseDirectiveZero() {
1845 CheckForValidSection();
1848 if (ParseAbsoluteExpression(NumBytes))
1852 if (getLexer().is(AsmToken::Comma)) {
1854 if (ParseAbsoluteExpression(Val))
1858 if (getLexer().isNot(AsmToken::EndOfStatement))
1859 return TokError("unexpected token in '.zero' directive");
1863 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
1868 /// ParseDirectiveFill
1869 /// ::= .fill expression , expression , expression
1870 bool AsmParser::ParseDirectiveFill() {
1871 CheckForValidSection();
1874 if (ParseAbsoluteExpression(NumValues))
1877 if (getLexer().isNot(AsmToken::Comma))
1878 return TokError("unexpected token in '.fill' directive");
1882 if (ParseAbsoluteExpression(FillSize))
1885 if (getLexer().isNot(AsmToken::Comma))
1886 return TokError("unexpected token in '.fill' directive");
1890 if (ParseAbsoluteExpression(FillExpr))
1893 if (getLexer().isNot(AsmToken::EndOfStatement))
1894 return TokError("unexpected token in '.fill' directive");
1898 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1899 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1901 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1902 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
1907 /// ParseDirectiveOrg
1908 /// ::= .org expression [ , expression ]
1909 bool AsmParser::ParseDirectiveOrg() {
1910 CheckForValidSection();
1912 const MCExpr *Offset;
1913 if (ParseExpression(Offset))
1916 // Parse optional fill expression.
1917 int64_t FillExpr = 0;
1918 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1919 if (getLexer().isNot(AsmToken::Comma))
1920 return TokError("unexpected token in '.org' directive");
1923 if (ParseAbsoluteExpression(FillExpr))
1926 if (getLexer().isNot(AsmToken::EndOfStatement))
1927 return TokError("unexpected token in '.org' directive");
1932 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1933 // has to be relative to the current section.
1934 getStreamer().EmitValueToOffset(Offset, FillExpr);
1939 /// ParseDirectiveAlign
1940 /// ::= {.align, ...} expression [ , expression [ , expression ]]
1941 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1942 CheckForValidSection();
1944 SMLoc AlignmentLoc = getLexer().getLoc();
1946 if (ParseAbsoluteExpression(Alignment))
1950 bool HasFillExpr = false;
1951 int64_t FillExpr = 0;
1952 int64_t MaxBytesToFill = 0;
1953 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1954 if (getLexer().isNot(AsmToken::Comma))
1955 return TokError("unexpected token in directive");
1958 // The fill expression can be omitted while specifying a maximum number of
1959 // alignment bytes, e.g:
1961 if (getLexer().isNot(AsmToken::Comma)) {
1963 if (ParseAbsoluteExpression(FillExpr))
1967 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1968 if (getLexer().isNot(AsmToken::Comma))
1969 return TokError("unexpected token in directive");
1972 MaxBytesLoc = getLexer().getLoc();
1973 if (ParseAbsoluteExpression(MaxBytesToFill))
1976 if (getLexer().isNot(AsmToken::EndOfStatement))
1977 return TokError("unexpected token in directive");
1986 // Compute alignment in bytes.
1988 // FIXME: Diagnose overflow.
1989 if (Alignment >= 32) {
1990 Error(AlignmentLoc, "invalid alignment value");
1994 Alignment = 1ULL << Alignment;
1997 // Diagnose non-sensical max bytes to align.
1998 if (MaxBytesLoc.isValid()) {
1999 if (MaxBytesToFill < 1) {
2000 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2001 "many bytes, ignoring maximum bytes expression");
2005 if (MaxBytesToFill >= Alignment) {
2006 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2012 // Check whether we should use optimal code alignment for this .align
2014 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
2015 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2016 ValueSize == 1 && UseCodeAlign) {
2017 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
2019 // FIXME: Target specific behavior about how the "extra" bytes are filled.
2020 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2027 /// ParseDirectiveSymbolAttribute
2028 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
2029 bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
2030 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2033 SMLoc Loc = getTok().getLoc();
2035 if (ParseIdentifier(Name))
2036 return Error(Loc, "expected identifier in directive");
2038 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2040 // Assembler local symbols don't make any sense here. Complain loudly.
2041 if (Sym->isTemporary())
2042 return Error(Loc, "non-local symbol required in directive");
2044 getStreamer().EmitSymbolAttribute(Sym, Attr);
2046 if (getLexer().is(AsmToken::EndOfStatement))
2049 if (getLexer().isNot(AsmToken::Comma))
2050 return TokError("unexpected token in directive");
2059 /// ParseDirectiveComm
2060 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2061 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
2062 CheckForValidSection();
2064 SMLoc IDLoc = getLexer().getLoc();
2066 if (ParseIdentifier(Name))
2067 return TokError("expected identifier in directive");
2069 // Handle the identifier as the key symbol.
2070 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2072 if (getLexer().isNot(AsmToken::Comma))
2073 return TokError("unexpected token in directive");
2077 SMLoc SizeLoc = getLexer().getLoc();
2078 if (ParseAbsoluteExpression(Size))
2081 int64_t Pow2Alignment = 0;
2082 SMLoc Pow2AlignmentLoc;
2083 if (getLexer().is(AsmToken::Comma)) {
2085 Pow2AlignmentLoc = getLexer().getLoc();
2086 if (ParseAbsoluteExpression(Pow2Alignment))
2089 // If this target takes alignments in bytes (not log) validate and convert.
2090 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2091 if (!isPowerOf2_64(Pow2Alignment))
2092 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2093 Pow2Alignment = Log2_64(Pow2Alignment);
2097 if (getLexer().isNot(AsmToken::EndOfStatement))
2098 return TokError("unexpected token in '.comm' or '.lcomm' directive");
2102 // NOTE: a size of zero for a .comm should create a undefined symbol
2103 // but a size of .lcomm creates a bss symbol of size zero.
2105 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2106 "be less than zero");
2108 // NOTE: The alignment in the directive is a power of 2 value, the assembler
2109 // may internally end up wanting an alignment in bytes.
2110 // FIXME: Diagnose overflow.
2111 if (Pow2Alignment < 0)
2112 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2113 "alignment, can't be less than zero");
2115 if (!Sym->isUndefined())
2116 return Error(IDLoc, "invalid symbol redefinition");
2118 // '.lcomm' is equivalent to '.zerofill'.
2119 // Create the Symbol as a common or local common with Size and Pow2Alignment
2121 getStreamer().EmitZerofill(Ctx.getMachOSection(
2122 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2123 0, SectionKind::getBSS()),
2124 Sym, Size, 1 << Pow2Alignment);
2128 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
2132 /// ParseDirectiveAbort
2133 /// ::= .abort [... message ...]
2134 bool AsmParser::ParseDirectiveAbort() {
2135 // FIXME: Use loc from directive.
2136 SMLoc Loc = getLexer().getLoc();
2138 StringRef Str = ParseStringToEndOfStatement();
2139 if (getLexer().isNot(AsmToken::EndOfStatement))
2140 return TokError("unexpected token in '.abort' directive");
2145 Error(Loc, ".abort detected. Assembly stopping.");
2147 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
2148 // FIXME: Actually abort assembly here.
2153 /// ParseDirectiveInclude
2154 /// ::= .include "filename"
2155 bool AsmParser::ParseDirectiveInclude() {
2156 if (getLexer().isNot(AsmToken::String))
2157 return TokError("expected string in '.include' directive");
2159 std::string Filename = getTok().getString();
2160 SMLoc IncludeLoc = getLexer().getLoc();
2163 if (getLexer().isNot(AsmToken::EndOfStatement))
2164 return TokError("unexpected token in '.include' directive");
2166 // Strip the quotes.
2167 Filename = Filename.substr(1, Filename.size()-2);
2169 // Attempt to switch the lexer to the included file before consuming the end
2170 // of statement to avoid losing it when we switch.
2171 if (EnterIncludeFile(Filename)) {
2172 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
2179 /// ParseDirectiveIf
2180 /// ::= .if expression
2181 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
2182 TheCondStack.push_back(TheCondState);
2183 TheCondState.TheCond = AsmCond::IfCond;
2184 if(TheCondState.Ignore) {
2185 EatToEndOfStatement();
2189 if (ParseAbsoluteExpression(ExprValue))
2192 if (getLexer().isNot(AsmToken::EndOfStatement))
2193 return TokError("unexpected token in '.if' directive");
2197 TheCondState.CondMet = ExprValue;
2198 TheCondState.Ignore = !TheCondState.CondMet;
2204 bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2206 TheCondStack.push_back(TheCondState);
2207 TheCondState.TheCond = AsmCond::IfCond;
2209 if (TheCondState.Ignore) {
2210 EatToEndOfStatement();
2212 if (ParseIdentifier(Name))
2213 return TokError("expected identifier after '.ifdef'");
2217 MCSymbol *Sym = getContext().LookupSymbol(Name);
2220 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2222 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2223 TheCondState.Ignore = !TheCondState.CondMet;
2229 /// ParseDirectiveElseIf
2230 /// ::= .elseif expression
2231 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2232 if (TheCondState.TheCond != AsmCond::IfCond &&
2233 TheCondState.TheCond != AsmCond::ElseIfCond)
2234 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2236 TheCondState.TheCond = AsmCond::ElseIfCond;
2238 bool LastIgnoreState = false;
2239 if (!TheCondStack.empty())
2240 LastIgnoreState = TheCondStack.back().Ignore;
2241 if (LastIgnoreState || TheCondState.CondMet) {
2242 TheCondState.Ignore = true;
2243 EatToEndOfStatement();
2247 if (ParseAbsoluteExpression(ExprValue))
2250 if (getLexer().isNot(AsmToken::EndOfStatement))
2251 return TokError("unexpected token in '.elseif' directive");
2254 TheCondState.CondMet = ExprValue;
2255 TheCondState.Ignore = !TheCondState.CondMet;
2261 /// ParseDirectiveElse
2263 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
2264 if (getLexer().isNot(AsmToken::EndOfStatement))
2265 return TokError("unexpected token in '.else' directive");
2269 if (TheCondState.TheCond != AsmCond::IfCond &&
2270 TheCondState.TheCond != AsmCond::ElseIfCond)
2271 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2273 TheCondState.TheCond = AsmCond::ElseCond;
2274 bool LastIgnoreState = false;
2275 if (!TheCondStack.empty())
2276 LastIgnoreState = TheCondStack.back().Ignore;
2277 if (LastIgnoreState || TheCondState.CondMet)
2278 TheCondState.Ignore = true;
2280 TheCondState.Ignore = false;
2285 /// ParseDirectiveEndIf
2287 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
2288 if (getLexer().isNot(AsmToken::EndOfStatement))
2289 return TokError("unexpected token in '.endif' directive");
2293 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2294 TheCondStack.empty())
2295 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2297 if (!TheCondStack.empty()) {
2298 TheCondState = TheCondStack.back();
2299 TheCondStack.pop_back();
2305 /// ParseDirectiveFile
2306 /// ::= .file [number] filename
2307 /// ::= .file number directory filename
2308 bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
2309 // FIXME: I'm not sure what this is.
2310 int64_t FileNumber = -1;
2311 SMLoc FileNumberLoc = getLexer().getLoc();
2312 if (getLexer().is(AsmToken::Integer)) {
2313 FileNumber = getTok().getIntVal();
2317 return TokError("file number less than one");
2320 if (getLexer().isNot(AsmToken::String))
2321 return TokError("unexpected token in '.file' directive");
2323 // Usually the directory and filename together, otherwise just the directory.
2324 StringRef Path = getTok().getString();
2325 Path = Path.substr(1, Path.size()-2);
2328 StringRef Directory;
2330 if (getLexer().is(AsmToken::String)) {
2331 if (FileNumber == -1)
2332 return TokError("explicit path specified, but no file number");
2333 Filename = getTok().getString();
2334 Filename = Filename.substr(1, Filename.size()-2);
2341 if (getLexer().isNot(AsmToken::EndOfStatement))
2342 return TokError("unexpected token in '.file' directive");
2344 if (FileNumber == -1)
2345 getStreamer().EmitFileDirective(Filename);
2347 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2348 Error(FileNumberLoc, "file number already allocated");
2354 /// ParseDirectiveLine
2355 /// ::= .line [number]
2356 bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
2357 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2358 if (getLexer().isNot(AsmToken::Integer))
2359 return TokError("unexpected token in '.line' directive");
2361 int64_t LineNumber = getTok().getIntVal();
2365 // FIXME: Do something with the .line.
2368 if (getLexer().isNot(AsmToken::EndOfStatement))
2369 return TokError("unexpected token in '.line' directive");
2375 /// ParseDirectiveLoc
2376 /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2377 /// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2378 /// The first number is a file number, must have been previously assigned with
2379 /// a .file directive, the second number is the line number and optionally the
2380 /// third number is a column position (zero if not specified). The remaining
2381 /// optional items are .loc sub-directives.
2382 bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
2384 if (getLexer().isNot(AsmToken::Integer))
2385 return TokError("unexpected token in '.loc' directive");
2386 int64_t FileNumber = getTok().getIntVal();
2388 return TokError("file number less than one in '.loc' directive");
2389 if (!getContext().isValidDwarfFileNumber(FileNumber))
2390 return TokError("unassigned file number in '.loc' directive");
2393 int64_t LineNumber = 0;
2394 if (getLexer().is(AsmToken::Integer)) {
2395 LineNumber = getTok().getIntVal();
2397 return TokError("line number less than one in '.loc' directive");
2401 int64_t ColumnPos = 0;
2402 if (getLexer().is(AsmToken::Integer)) {
2403 ColumnPos = getTok().getIntVal();
2405 return TokError("column position less than zero in '.loc' directive");
2409 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2411 int64_t Discriminator = 0;
2412 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2414 if (getLexer().is(AsmToken::EndOfStatement))
2418 SMLoc Loc = getTok().getLoc();
2419 if (getParser().ParseIdentifier(Name))
2420 return TokError("unexpected token in '.loc' directive");
2422 if (Name == "basic_block")
2423 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2424 else if (Name == "prologue_end")
2425 Flags |= DWARF2_FLAG_PROLOGUE_END;
2426 else if (Name == "epilogue_begin")
2427 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2428 else if (Name == "is_stmt") {
2429 SMLoc Loc = getTok().getLoc();
2430 const MCExpr *Value;
2431 if (getParser().ParseExpression(Value))
2433 // The expression must be the constant 0 or 1.
2434 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2435 int Value = MCE->getValue();
2437 Flags &= ~DWARF2_FLAG_IS_STMT;
2438 else if (Value == 1)
2439 Flags |= DWARF2_FLAG_IS_STMT;
2441 return Error(Loc, "is_stmt value not 0 or 1");
2444 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2447 else if (Name == "isa") {
2448 SMLoc Loc = getTok().getLoc();
2449 const MCExpr *Value;
2450 if (getParser().ParseExpression(Value))
2452 // The expression must be a constant greater or equal to 0.
2453 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2454 int Value = MCE->getValue();
2456 return Error(Loc, "isa number less than zero");
2460 return Error(Loc, "isa number not a constant value");
2463 else if (Name == "discriminator") {
2464 if (getParser().ParseAbsoluteExpression(Discriminator))
2468 return Error(Loc, "unknown sub-directive in '.loc' directive");
2471 if (getLexer().is(AsmToken::EndOfStatement))
2476 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2477 Isa, Discriminator, StringRef());
2482 /// ParseDirectiveStabs
2483 /// ::= .stabs string, number, number, number
2484 bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2485 SMLoc DirectiveLoc) {
2486 return TokError("unsupported directive '" + Directive + "'");
2489 /// ParseDirectiveCFISections
2490 /// ::= .cfi_sections section [, section]
2491 bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2492 SMLoc DirectiveLoc) {
2497 if (getParser().ParseIdentifier(Name))
2498 return TokError("Expected an identifier");
2500 if (Name == ".eh_frame")
2502 else if (Name == ".debug_frame")
2505 if (getLexer().is(AsmToken::Comma)) {
2508 if (getParser().ParseIdentifier(Name))
2509 return TokError("Expected an identifier");
2511 if (Name == ".eh_frame")
2513 else if (Name == ".debug_frame")
2517 getStreamer().EmitCFISections(EH, Debug);
2522 /// ParseDirectiveCFIStartProc
2523 /// ::= .cfi_startproc
2524 bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2525 SMLoc DirectiveLoc) {
2526 getStreamer().EmitCFIStartProc();
2530 /// ParseDirectiveCFIEndProc
2531 /// ::= .cfi_endproc
2532 bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
2533 getStreamer().EmitCFIEndProc();
2537 /// ParseRegisterOrRegisterNumber - parse register name or number.
2538 bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2539 SMLoc DirectiveLoc) {
2542 if (getLexer().isNot(AsmToken::Integer)) {
2543 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2546 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
2548 return getParser().ParseAbsoluteExpression(Register);
2553 /// ParseDirectiveCFIDefCfa
2554 /// ::= .cfi_def_cfa register, offset
2555 bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2556 SMLoc DirectiveLoc) {
2557 int64_t Register = 0;
2558 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2561 if (getLexer().isNot(AsmToken::Comma))
2562 return TokError("unexpected token in directive");
2566 if (getParser().ParseAbsoluteExpression(Offset))
2569 getStreamer().EmitCFIDefCfa(Register, Offset);
2573 /// ParseDirectiveCFIDefCfaOffset
2574 /// ::= .cfi_def_cfa_offset offset
2575 bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2576 SMLoc DirectiveLoc) {
2578 if (getParser().ParseAbsoluteExpression(Offset))
2581 getStreamer().EmitCFIDefCfaOffset(Offset);
2585 /// ParseDirectiveCFIAdjustCfaOffset
2586 /// ::= .cfi_adjust_cfa_offset adjustment
2587 bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2588 SMLoc DirectiveLoc) {
2589 int64_t Adjustment = 0;
2590 if (getParser().ParseAbsoluteExpression(Adjustment))
2593 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2597 /// ParseDirectiveCFIDefCfaRegister
2598 /// ::= .cfi_def_cfa_register register
2599 bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2600 SMLoc DirectiveLoc) {
2601 int64_t Register = 0;
2602 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2605 getStreamer().EmitCFIDefCfaRegister(Register);
2609 /// ParseDirectiveCFIOffset
2610 /// ::= .cfi_offset register, offset
2611 bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2612 int64_t Register = 0;
2615 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2618 if (getLexer().isNot(AsmToken::Comma))
2619 return TokError("unexpected token in directive");
2622 if (getParser().ParseAbsoluteExpression(Offset))
2625 getStreamer().EmitCFIOffset(Register, Offset);
2629 /// ParseDirectiveCFIRelOffset
2630 /// ::= .cfi_rel_offset register, offset
2631 bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2632 SMLoc DirectiveLoc) {
2633 int64_t Register = 0;
2635 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2638 if (getLexer().isNot(AsmToken::Comma))
2639 return TokError("unexpected token in directive");
2643 if (getParser().ParseAbsoluteExpression(Offset))
2646 getStreamer().EmitCFIRelOffset(Register, Offset);
2650 static bool isValidEncoding(int64_t Encoding) {
2651 if (Encoding & ~0xff)
2654 if (Encoding == dwarf::DW_EH_PE_omit)
2657 const unsigned Format = Encoding & 0xf;
2658 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2659 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2660 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2661 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2664 const unsigned Application = Encoding & 0x70;
2665 if (Application != dwarf::DW_EH_PE_absptr &&
2666 Application != dwarf::DW_EH_PE_pcrel)
2672 /// ParseDirectiveCFIPersonalityOrLsda
2673 /// ::= .cfi_personality encoding, [symbol_name]
2674 /// ::= .cfi_lsda encoding, [symbol_name]
2675 bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
2676 SMLoc DirectiveLoc) {
2677 int64_t Encoding = 0;
2678 if (getParser().ParseAbsoluteExpression(Encoding))
2680 if (Encoding == dwarf::DW_EH_PE_omit)
2683 if (!isValidEncoding(Encoding))
2684 return TokError("unsupported encoding.");
2686 if (getLexer().isNot(AsmToken::Comma))
2687 return TokError("unexpected token in directive");
2691 if (getParser().ParseIdentifier(Name))
2692 return TokError("expected identifier in directive");
2694 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2696 if (IDVal == ".cfi_personality")
2697 getStreamer().EmitCFIPersonality(Sym, Encoding);
2699 assert(IDVal == ".cfi_lsda");
2700 getStreamer().EmitCFILsda(Sym, Encoding);
2705 /// ParseDirectiveCFIRememberState
2706 /// ::= .cfi_remember_state
2707 bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2708 SMLoc DirectiveLoc) {
2709 getStreamer().EmitCFIRememberState();
2713 /// ParseDirectiveCFIRestoreState
2714 /// ::= .cfi_remember_state
2715 bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2716 SMLoc DirectiveLoc) {
2717 getStreamer().EmitCFIRestoreState();
2721 /// ParseDirectiveCFISameValue
2722 /// ::= .cfi_same_value register
2723 bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2724 SMLoc DirectiveLoc) {
2725 int64_t Register = 0;
2727 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2730 getStreamer().EmitCFISameValue(Register);
2735 /// ParseDirectiveMacrosOnOff
2738 bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2739 SMLoc DirectiveLoc) {
2740 if (getLexer().isNot(AsmToken::EndOfStatement))
2741 return Error(getLexer().getLoc(),
2742 "unexpected token in '" + Directive + "' directive");
2744 getParser().MacrosEnabled = Directive == ".macros_on";
2749 /// ParseDirectiveMacro
2750 /// ::= .macro name [parameters]
2751 bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2752 SMLoc DirectiveLoc) {
2754 if (getParser().ParseIdentifier(Name))
2755 return TokError("expected identifier in directive");
2757 std::vector<StringRef> Parameters;
2758 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2760 StringRef Parameter;
2761 if (getParser().ParseIdentifier(Parameter))
2762 return TokError("expected identifier in directive");
2763 Parameters.push_back(Parameter);
2765 if (getLexer().isNot(AsmToken::Comma))
2771 if (getLexer().isNot(AsmToken::EndOfStatement))
2772 return TokError("unexpected token in '.macro' directive");
2774 // Eat the end of statement.
2777 AsmToken EndToken, StartToken = getTok();
2779 // Lex the macro definition.
2781 // Check whether we have reached the end of the file.
2782 if (getLexer().is(AsmToken::Eof))
2783 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2785 // Otherwise, check whether we have reach the .endmacro.
2786 if (getLexer().is(AsmToken::Identifier) &&
2787 (getTok().getIdentifier() == ".endm" ||
2788 getTok().getIdentifier() == ".endmacro")) {
2789 EndToken = getTok();
2791 if (getLexer().isNot(AsmToken::EndOfStatement))
2792 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2797 // Otherwise, scan til the end of the statement.
2798 getParser().EatToEndOfStatement();
2801 if (getParser().MacroMap.lookup(Name)) {
2802 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2805 const char *BodyStart = StartToken.getLoc().getPointer();
2806 const char *BodyEnd = EndToken.getLoc().getPointer();
2807 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2808 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
2812 /// ParseDirectiveEndMacro
2815 bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2816 SMLoc DirectiveLoc) {
2817 if (getLexer().isNot(AsmToken::EndOfStatement))
2818 return TokError("unexpected token in '" + Directive + "' directive");
2820 // If we are inside a macro instantiation, terminate the current
2822 if (!getParser().ActiveMacros.empty()) {
2823 getParser().HandleMacroExit();
2827 // Otherwise, this .endmacro is a stray entry in the file; well formed
2828 // .endmacro directives are handled during the macro definition parsing.
2829 return TokError("unexpected '" + Directive + "' in file, "
2830 "no current macro definition");
2833 bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
2834 getParser().CheckForValidSection();
2836 const MCExpr *Value;
2838 if (getParser().ParseExpression(Value))
2841 if (getLexer().isNot(AsmToken::EndOfStatement))
2842 return TokError("unexpected token in directive");
2844 if (DirName[1] == 's')
2845 getStreamer().EmitSLEB128Value(Value);
2847 getStreamer().EmitULEB128Value(Value);
2853 /// \brief Create an MCAsmParser instance.
2854 MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
2855 MCContext &C, MCStreamer &Out,
2856 const MCAsmInfo &MAI) {
2857 return new AsmParser(SM, C, Out, MAI);