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 virtual bool Error(SMLoc L, const Twine &Msg);
150 const AsmToken &Lex();
152 bool ParseExpression(const MCExpr *&Res);
153 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
154 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
155 virtual bool ParseAbsoluteExpression(int64_t &Res);
160 void CheckForValidSection();
162 bool ParseStatement();
163 void EatToEndOfLine();
164 bool ParseCppHashLineFilenameComment(const SMLoc &L);
166 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
167 bool expandMacro(SmallString<256> &Buf, StringRef Body,
168 const std::vector<StringRef> &Parameters,
169 const std::vector<std::vector<AsmToken> > &A,
171 void HandleMacroExit();
173 void PrintMacroInstantiations();
174 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type,
175 bool ShowLine = true) const {
176 SrcMgr.PrintMessage(Loc, Msg, Type, ShowLine);
178 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
180 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
181 bool EnterIncludeFile(const std::string &Filename);
183 /// \brief Reset the current lexer position to that given by \arg Loc. The
184 /// current token is not set; clients should ensure Lex() is called
186 void JumpToLoc(SMLoc Loc);
188 void EatToEndOfStatement();
190 /// \brief Parse up to the end of statement and a return the contents from the
191 /// current token until the end of the statement; the current token on exit
192 /// will be either the EndOfStatement or EOF.
193 StringRef ParseStringToEndOfStatement();
195 bool ParseAssignment(StringRef Name, bool allow_redef);
197 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
198 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
199 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
200 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
202 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
203 /// and set \arg Res to the identifier contents.
204 bool ParseIdentifier(StringRef &Res);
206 // Directive Parsing.
208 // ".ascii", ".asciiz", ".string"
209 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
210 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
211 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
212 bool ParseDirectiveFill(); // ".fill"
213 bool ParseDirectiveSpace(); // ".space"
214 bool ParseDirectiveZero(); // ".zero"
215 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
216 bool ParseDirectiveOrg(); // ".org"
217 // ".align{,32}", ".p2align{,w,l}"
218 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
220 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
221 /// accepts a single symbol (which should be a label or an external).
222 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
224 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
226 bool ParseDirectiveAbort(); // ".abort"
227 bool ParseDirectiveInclude(); // ".include"
229 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
230 // ".ifdef" or ".ifndef", depending on expect_defined
231 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
232 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
233 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
234 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
236 /// ParseEscapedString - Parse the current token as a string which may include
237 /// escaped characters and return the string contents.
238 bool ParseEscapedString(std::string &Data);
240 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
241 MCSymbolRefExpr::VariantKind Variant);
244 /// \brief Generic implementations of directive handling, etc. which is shared
245 /// (or the default, at least) for all assembler parser.
246 class GenericAsmParser : public MCAsmParserExtension {
247 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
248 void AddDirectiveHandler(StringRef Directive) {
249 getParser().AddDirectiveHandler(this, Directive,
250 HandleDirective<GenericAsmParser, Handler>);
253 GenericAsmParser() {}
255 AsmParser &getParser() {
256 return (AsmParser&) this->MCAsmParserExtension::getParser();
259 virtual void Initialize(MCAsmParser &Parser) {
260 // Call the base implementation.
261 this->MCAsmParserExtension::Initialize(Parser);
263 // Debugging directives.
264 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
270 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
278 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
279 ".cfi_def_cfa_offset");
280 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
281 ".cfi_adjust_cfa_offset");
282 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
283 ".cfi_def_cfa_register");
284 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
286 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
289 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
291 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
293 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
295 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
297 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
300 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
302 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
304 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
305 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
306 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
308 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
309 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
312 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
314 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
315 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
316 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
317 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
318 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
319 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
320 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
321 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
322 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
323 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
324 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
325 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
326 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
327 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
328 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
329 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
330 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
332 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
333 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
334 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
336 bool ParseDirectiveLEB128(StringRef, SMLoc);
343 extern MCAsmParserExtension *createDarwinAsmParser();
344 extern MCAsmParserExtension *createELFAsmParser();
345 extern MCAsmParserExtension *createCOFFAsmParser();
349 enum { DEFAULT_ADDRSPACE = 0 };
351 AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
352 MCStreamer &_Out, const MCAsmInfo &_MAI)
353 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
354 GenericParser(new GenericAsmParser), PlatformParser(0),
355 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0) {
356 // Save the old handler.
357 SavedDiagHandler = SrcMgr.getDiagHandler();
358 SavedDiagContext = SrcMgr.getDiagContext();
359 // Set our own handler which calls the saved handler.
360 SrcMgr.setDiagHandler(DiagHandler, this);
361 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
363 // Initialize the generic parser.
364 GenericParser->Initialize(*this);
366 // Initialize the platform / file format parser.
368 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
370 if (_MAI.hasMicrosoftFastStdCallMangling()) {
371 PlatformParser = createCOFFAsmParser();
372 PlatformParser->Initialize(*this);
373 } else if (_MAI.hasSubsectionsViaSymbols()) {
374 PlatformParser = createDarwinAsmParser();
375 PlatformParser->Initialize(*this);
377 PlatformParser = createELFAsmParser();
378 PlatformParser->Initialize(*this);
382 AsmParser::~AsmParser() {
383 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
385 // Destroy any macros.
386 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
387 ie = MacroMap.end(); it != ie; ++it)
388 delete it->getValue();
390 delete PlatformParser;
391 delete GenericParser;
394 void AsmParser::PrintMacroInstantiations() {
395 // Print the active macro instantiation stack.
396 for (std::vector<MacroInstantiation*>::const_reverse_iterator
397 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
398 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
402 bool AsmParser::Warning(SMLoc L, const Twine &Msg) {
403 if (FatalAssemblerWarnings)
404 return Error(L, Msg);
405 PrintMessage(L, Msg, "warning");
406 PrintMacroInstantiations();
410 bool AsmParser::Error(SMLoc L, const Twine &Msg) {
412 PrintMessage(L, Msg, "error");
413 PrintMacroInstantiations();
417 bool AsmParser::EnterIncludeFile(const std::string &Filename) {
418 std::string IncludedFile;
419 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
425 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
430 void AsmParser::JumpToLoc(SMLoc Loc) {
431 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
432 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
435 const AsmToken &AsmParser::Lex() {
436 const AsmToken *tok = &Lexer.Lex();
438 if (tok->is(AsmToken::Eof)) {
439 // If this is the end of an included file, pop the parent file off the
441 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
442 if (ParentIncludeLoc != SMLoc()) {
443 JumpToLoc(ParentIncludeLoc);
448 if (tok->is(AsmToken::Error))
449 Error(Lexer.getErrLoc(), Lexer.getErr());
454 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
455 // Create the initial section, if requested.
456 if (!NoInitialTextSection)
463 AsmCond StartingCondState = TheCondState;
465 // While we have input, parse each statement.
466 while (Lexer.isNot(AsmToken::Eof)) {
467 if (!ParseStatement()) continue;
469 // We had an error, validate that one was emitted and recover by skipping to
471 assert(HadError && "Parse statement returned an error, but none emitted!");
472 EatToEndOfStatement();
475 if (TheCondState.TheCond != StartingCondState.TheCond ||
476 TheCondState.Ignore != StartingCondState.Ignore)
477 return TokError("unmatched .ifs or .elses");
479 // Check to see there are no empty DwarfFile slots.
480 const std::vector<MCDwarfFile *> &MCDwarfFiles =
481 getContext().getMCDwarfFiles();
482 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
483 if (!MCDwarfFiles[i])
484 TokError("unassigned file number: " + Twine(i) + " for .file directives");
487 // Check to see that all assembler local symbols were actually defined.
488 // Targets that don't do subsections via symbols may not want this, though,
489 // so conservatively exclude them. Only do this if we're finalizing, though,
490 // as otherwise we won't necessarilly have seen everything yet.
491 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
492 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
493 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
496 MCSymbol *Sym = i->getValue();
497 // Variable symbols may not be marked as defined, so check those
498 // explicitly. If we know it's a variable, we have a definition for
499 // the purposes of this check.
500 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
501 // FIXME: We would really like to refer back to where the symbol was
502 // first referenced for a source location. We need to add something
503 // to track that. Currently, we just point to the end of the file.
504 PrintMessage(getLexer().getLoc(), "assembler local symbol '" +
505 Sym->getName() + "' not defined", "error", false);
510 // Finalize the output stream if there are no errors and if the client wants
512 if (!HadError && !NoFinalize)
518 void AsmParser::CheckForValidSection() {
519 if (!getStreamer().getCurrentSection()) {
520 TokError("expected section directive before assembly directive");
521 Out.SwitchSection(Ctx.getMachOSection(
523 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
524 0, SectionKind::getText()));
528 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
529 void AsmParser::EatToEndOfStatement() {
530 while (Lexer.isNot(AsmToken::EndOfStatement) &&
531 Lexer.isNot(AsmToken::Eof))
535 if (Lexer.is(AsmToken::EndOfStatement))
539 StringRef AsmParser::ParseStringToEndOfStatement() {
540 const char *Start = getTok().getLoc().getPointer();
542 while (Lexer.isNot(AsmToken::EndOfStatement) &&
543 Lexer.isNot(AsmToken::Eof))
546 const char *End = getTok().getLoc().getPointer();
547 return StringRef(Start, End - Start);
550 /// ParseParenExpr - Parse a paren expression and return it.
551 /// NOTE: This assumes the leading '(' has already been consumed.
553 /// parenexpr ::= expr)
555 bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
556 if (ParseExpression(Res)) return true;
557 if (Lexer.isNot(AsmToken::RParen))
558 return TokError("expected ')' in parentheses expression");
559 EndLoc = Lexer.getLoc();
564 /// ParseBracketExpr - Parse a bracket expression and return it.
565 /// NOTE: This assumes the leading '[' has already been consumed.
567 /// bracketexpr ::= expr]
569 bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
570 if (ParseExpression(Res)) return true;
571 if (Lexer.isNot(AsmToken::RBrac))
572 return TokError("expected ']' in brackets expression");
573 EndLoc = Lexer.getLoc();
578 /// ParsePrimaryExpr - Parse a primary expression and return it.
579 /// primaryexpr ::= (parenexpr
580 /// primaryexpr ::= symbol
581 /// primaryexpr ::= number
582 /// primaryexpr ::= '.'
583 /// primaryexpr ::= ~,+,- primaryexpr
584 bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
585 switch (Lexer.getKind()) {
587 return TokError("unknown token in expression");
588 // If we have an error assume that we've already handled it.
589 case AsmToken::Error:
591 case AsmToken::Exclaim:
592 Lex(); // Eat the operator.
593 if (ParsePrimaryExpr(Res, EndLoc))
595 Res = MCUnaryExpr::CreateLNot(Res, getContext());
597 case AsmToken::Dollar:
598 case AsmToken::String:
599 case AsmToken::Identifier: {
600 EndLoc = Lexer.getLoc();
602 StringRef Identifier;
603 if (ParseIdentifier(Identifier))
606 // This is a symbol reference.
607 std::pair<StringRef, StringRef> Split = Identifier.split('@');
608 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
610 // Lookup the symbol variant if used.
611 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
612 if (Split.first.size() != Identifier.size()) {
613 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
614 if (Variant == MCSymbolRefExpr::VK_Invalid) {
615 Variant = MCSymbolRefExpr::VK_None;
616 return TokError("invalid variant '" + Split.second + "'");
620 // If this is an absolute variable reference, substitute it now to preserve
621 // semantics in the face of reassignment.
622 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
624 return Error(EndLoc, "unexpected modifier on variable reference");
626 Res = Sym->getVariableValue();
630 // Otherwise create a symbol ref.
631 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
634 case AsmToken::Integer: {
635 SMLoc Loc = getTok().getLoc();
636 int64_t IntVal = getTok().getIntVal();
637 Res = MCConstantExpr::Create(IntVal, getContext());
638 EndLoc = Lexer.getLoc();
640 // Look for 'b' or 'f' following an Integer as a directional label
641 if (Lexer.getKind() == AsmToken::Identifier) {
642 StringRef IDVal = getTok().getString();
643 if (IDVal == "f" || IDVal == "b"){
644 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
645 IDVal == "f" ? 1 : 0);
646 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
648 if(IDVal == "b" && Sym->isUndefined())
649 return Error(Loc, "invalid reference to undefined symbol");
650 EndLoc = Lexer.getLoc();
651 Lex(); // Eat identifier.
656 case AsmToken::Real: {
657 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
658 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
659 Res = MCConstantExpr::Create(IntVal, getContext());
663 case AsmToken::Dot: {
664 // This is a '.' reference, which references the current PC. Emit a
665 // temporary label to the streamer and refer to it.
666 MCSymbol *Sym = Ctx.CreateTempSymbol();
668 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
669 EndLoc = Lexer.getLoc();
670 Lex(); // Eat identifier.
673 case AsmToken::LParen:
674 Lex(); // Eat the '('.
675 return ParseParenExpr(Res, EndLoc);
676 case AsmToken::LBrac:
677 if (!PlatformParser->HasBracketExpressions())
678 return TokError("brackets expression not supported on this target");
679 Lex(); // Eat the '['.
680 return ParseBracketExpr(Res, EndLoc);
681 case AsmToken::Minus:
682 Lex(); // Eat the operator.
683 if (ParsePrimaryExpr(Res, EndLoc))
685 Res = MCUnaryExpr::CreateMinus(Res, getContext());
688 Lex(); // Eat the operator.
689 if (ParsePrimaryExpr(Res, EndLoc))
691 Res = MCUnaryExpr::CreatePlus(Res, getContext());
693 case AsmToken::Tilde:
694 Lex(); // Eat the operator.
695 if (ParsePrimaryExpr(Res, EndLoc))
697 Res = MCUnaryExpr::CreateNot(Res, getContext());
702 bool AsmParser::ParseExpression(const MCExpr *&Res) {
704 return ParseExpression(Res, EndLoc);
708 AsmParser::ApplyModifierToExpr(const MCExpr *E,
709 MCSymbolRefExpr::VariantKind Variant) {
710 // Recurse over the given expression, rebuilding it to apply the given variant
711 // if there is exactly one symbol.
712 switch (E->getKind()) {
714 case MCExpr::Constant:
717 case MCExpr::SymbolRef: {
718 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
720 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
721 TokError("invalid variant on expression '" +
722 getTok().getIdentifier() + "' (already modified)");
726 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
729 case MCExpr::Unary: {
730 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
731 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
734 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
737 case MCExpr::Binary: {
738 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
739 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
740 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
745 if (!LHS) LHS = BE->getLHS();
746 if (!RHS) RHS = BE->getRHS();
748 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
752 assert(0 && "Invalid expression kind!");
756 /// ParseExpression - Parse an expression and return it.
758 /// expr ::= expr &&,|| expr -> lowest.
759 /// expr ::= expr |,^,&,! expr
760 /// expr ::= expr ==,!=,<>,<,<=,>,>= expr
761 /// expr ::= expr <<,>> expr
762 /// expr ::= expr +,- expr
763 /// expr ::= expr *,/,% expr -> highest.
764 /// expr ::= primaryexpr
766 bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
767 // Parse the expression.
769 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
772 // As a special case, we support 'a op b @ modifier' by rewriting the
773 // expression to include the modifier. This is inefficient, but in general we
774 // expect users to use 'a@modifier op b'.
775 if (Lexer.getKind() == AsmToken::At) {
778 if (Lexer.isNot(AsmToken::Identifier))
779 return TokError("unexpected symbol modifier following '@'");
781 MCSymbolRefExpr::VariantKind Variant =
782 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
783 if (Variant == MCSymbolRefExpr::VK_Invalid)
784 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
786 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
788 return TokError("invalid modifier '" + getTok().getIdentifier() +
789 "' (no symbols present)");
797 // Try to constant fold it up front, if possible.
799 if (Res->EvaluateAsAbsolute(Value))
800 Res = MCConstantExpr::Create(Value, getContext());
805 bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
807 return ParseParenExpr(Res, EndLoc) ||
808 ParseBinOpRHS(1, Res, EndLoc);
811 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
814 SMLoc StartLoc = Lexer.getLoc();
815 if (ParseExpression(Expr))
818 if (!Expr->EvaluateAsAbsolute(Res))
819 return Error(StartLoc, "expected absolute expression");
824 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
825 MCBinaryExpr::Opcode &Kind) {
828 return 0; // not a binop.
830 // Lowest Precedence: &&, ||
831 case AsmToken::AmpAmp:
832 Kind = MCBinaryExpr::LAnd;
834 case AsmToken::PipePipe:
835 Kind = MCBinaryExpr::LOr;
839 // Low Precedence: |, &, ^
841 // FIXME: gas seems to support '!' as an infix operator?
843 Kind = MCBinaryExpr::Or;
845 case AsmToken::Caret:
846 Kind = MCBinaryExpr::Xor;
849 Kind = MCBinaryExpr::And;
852 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
853 case AsmToken::EqualEqual:
854 Kind = MCBinaryExpr::EQ;
856 case AsmToken::ExclaimEqual:
857 case AsmToken::LessGreater:
858 Kind = MCBinaryExpr::NE;
861 Kind = MCBinaryExpr::LT;
863 case AsmToken::LessEqual:
864 Kind = MCBinaryExpr::LTE;
866 case AsmToken::Greater:
867 Kind = MCBinaryExpr::GT;
869 case AsmToken::GreaterEqual:
870 Kind = MCBinaryExpr::GTE;
873 // Intermediate Precedence: <<, >>
874 case AsmToken::LessLess:
875 Kind = MCBinaryExpr::Shl;
877 case AsmToken::GreaterGreater:
878 Kind = MCBinaryExpr::Shr;
881 // High Intermediate Precedence: +, -
883 Kind = MCBinaryExpr::Add;
885 case AsmToken::Minus:
886 Kind = MCBinaryExpr::Sub;
889 // Highest Precedence: *, /, %
891 Kind = MCBinaryExpr::Mul;
893 case AsmToken::Slash:
894 Kind = MCBinaryExpr::Div;
896 case AsmToken::Percent:
897 Kind = MCBinaryExpr::Mod;
903 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
904 /// Res contains the LHS of the expression on input.
905 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
908 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
909 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
911 // If the next token is lower precedence than we are allowed to eat, return
912 // successfully with what we ate already.
913 if (TokPrec < Precedence)
918 // Eat the next primary expression.
920 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
922 // If BinOp binds less tightly with RHS than the operator after RHS, let
923 // the pending operator take RHS as its LHS.
924 MCBinaryExpr::Opcode Dummy;
925 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
926 if (TokPrec < NextTokPrec) {
927 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
930 // Merge LHS and RHS according to operator.
931 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
939 /// ::= EndOfStatement
940 /// ::= Label* Directive ...Operands... EndOfStatement
941 /// ::= Label* Identifier OperandList* EndOfStatement
942 bool AsmParser::ParseStatement() {
943 if (Lexer.is(AsmToken::EndOfStatement)) {
949 // Statements always start with an identifier or are a full line comment.
950 AsmToken ID = getTok();
951 SMLoc IDLoc = ID.getLoc();
953 int64_t LocalLabelVal = -1;
954 // A full line comment is a '#' as the first token.
955 if (Lexer.is(AsmToken::Hash))
956 return ParseCppHashLineFilenameComment(IDLoc);
958 // Allow an integer followed by a ':' as a directional local label.
959 if (Lexer.is(AsmToken::Integer)) {
960 LocalLabelVal = getTok().getIntVal();
961 if (LocalLabelVal < 0) {
962 if (!TheCondState.Ignore)
963 return TokError("unexpected token at start of statement");
967 IDVal = getTok().getString();
968 Lex(); // Consume the integer token to be used as an identifier token.
969 if (Lexer.getKind() != AsmToken::Colon) {
970 if (!TheCondState.Ignore)
971 return TokError("unexpected token at start of statement");
975 } else if (Lexer.is(AsmToken::Dot)) {
976 // Treat '.' as a valid identifier in this context.
980 } else if (ParseIdentifier(IDVal)) {
981 if (!TheCondState.Ignore)
982 return TokError("unexpected token at start of statement");
987 // Handle conditional assembly here before checking for skipping. We
988 // have to do this so that .endif isn't skipped in a ".if 0" block for
991 return ParseDirectiveIf(IDLoc);
992 if (IDVal == ".ifdef")
993 return ParseDirectiveIfdef(IDLoc, true);
994 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
995 return ParseDirectiveIfdef(IDLoc, false);
996 if (IDVal == ".elseif")
997 return ParseDirectiveElseIf(IDLoc);
998 if (IDVal == ".else")
999 return ParseDirectiveElse(IDLoc);
1000 if (IDVal == ".endif")
1001 return ParseDirectiveEndIf(IDLoc);
1003 // If we are in a ".if 0" block, ignore this statement.
1004 if (TheCondState.Ignore) {
1005 EatToEndOfStatement();
1009 // FIXME: Recurse on local labels?
1011 // See what kind of statement we have.
1012 switch (Lexer.getKind()) {
1013 case AsmToken::Colon: {
1014 CheckForValidSection();
1016 // identifier ':' -> Label.
1019 // Diagnose attempt to use '.' as a label.
1021 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1023 // Diagnose attempt to use a variable as a label.
1025 // FIXME: Diagnostics. Note the location of the definition as a label.
1026 // FIXME: This doesn't diagnose assignment to a symbol which has been
1027 // implicitly marked as external.
1029 if (LocalLabelVal == -1)
1030 Sym = getContext().GetOrCreateSymbol(IDVal);
1032 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
1033 if (!Sym->isUndefined() || Sym->isVariable())
1034 return Error(IDLoc, "invalid symbol redefinition");
1039 // Consume any end of statement token, if present, to avoid spurious
1040 // AddBlankLine calls().
1041 if (Lexer.is(AsmToken::EndOfStatement)) {
1043 if (Lexer.is(AsmToken::Eof))
1047 return ParseStatement();
1050 case AsmToken::Equal:
1051 // identifier '=' ... -> assignment statement
1054 return ParseAssignment(IDVal, true);
1056 default: // Normal instruction or directive.
1060 // If macros are enabled, check to see if this is a macro instantiation.
1062 if (const Macro *M = MacroMap.lookup(IDVal))
1063 return HandleMacroEntry(IDVal, IDLoc, M);
1065 // Otherwise, we have a normal instruction or directive.
1066 if (IDVal[0] == '.' && IDVal != ".") {
1067 // Assembler features
1068 if (IDVal == ".set" || IDVal == ".equ")
1069 return ParseDirectiveSet(IDVal, true);
1070 if (IDVal == ".equiv")
1071 return ParseDirectiveSet(IDVal, false);
1075 if (IDVal == ".ascii")
1076 return ParseDirectiveAscii(IDVal, false);
1077 if (IDVal == ".asciz" || IDVal == ".string")
1078 return ParseDirectiveAscii(IDVal, true);
1080 if (IDVal == ".byte")
1081 return ParseDirectiveValue(1);
1082 if (IDVal == ".short")
1083 return ParseDirectiveValue(2);
1084 if (IDVal == ".value")
1085 return ParseDirectiveValue(2);
1086 if (IDVal == ".2byte")
1087 return ParseDirectiveValue(2);
1088 if (IDVal == ".long")
1089 return ParseDirectiveValue(4);
1090 if (IDVal == ".int")
1091 return ParseDirectiveValue(4);
1092 if (IDVal == ".4byte")
1093 return ParseDirectiveValue(4);
1094 if (IDVal == ".quad")
1095 return ParseDirectiveValue(8);
1096 if (IDVal == ".8byte")
1097 return ParseDirectiveValue(8);
1098 if (IDVal == ".single" || IDVal == ".float")
1099 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1100 if (IDVal == ".double")
1101 return ParseDirectiveRealValue(APFloat::IEEEdouble);
1103 if (IDVal == ".align") {
1104 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1105 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1107 if (IDVal == ".align32") {
1108 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1109 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1111 if (IDVal == ".balign")
1112 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1113 if (IDVal == ".balignw")
1114 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1115 if (IDVal == ".balignl")
1116 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1117 if (IDVal == ".p2align")
1118 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1119 if (IDVal == ".p2alignw")
1120 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1121 if (IDVal == ".p2alignl")
1122 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1124 if (IDVal == ".org")
1125 return ParseDirectiveOrg();
1127 if (IDVal == ".fill")
1128 return ParseDirectiveFill();
1129 if (IDVal == ".space" || IDVal == ".skip")
1130 return ParseDirectiveSpace();
1131 if (IDVal == ".zero")
1132 return ParseDirectiveZero();
1134 // Symbol attribute directives
1136 if (IDVal == ".globl" || IDVal == ".global")
1137 return ParseDirectiveSymbolAttribute(MCSA_Global);
1138 if (IDVal == ".indirect_symbol")
1139 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
1140 if (IDVal == ".lazy_reference")
1141 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
1142 if (IDVal == ".no_dead_strip")
1143 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1144 if (IDVal == ".symbol_resolver")
1145 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1146 if (IDVal == ".private_extern")
1147 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1148 if (IDVal == ".reference")
1149 return ParseDirectiveSymbolAttribute(MCSA_Reference);
1150 if (IDVal == ".weak_definition")
1151 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1152 if (IDVal == ".weak_reference")
1153 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
1154 if (IDVal == ".weak_def_can_be_hidden")
1155 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1157 if (IDVal == ".comm" || IDVal == ".common")
1158 return ParseDirectiveComm(/*IsLocal=*/false);
1159 if (IDVal == ".lcomm")
1160 return ParseDirectiveComm(/*IsLocal=*/true);
1162 if (IDVal == ".abort")
1163 return ParseDirectiveAbort();
1164 if (IDVal == ".include")
1165 return ParseDirectiveInclude();
1167 if (IDVal == ".code16")
1168 return TokError(Twine(IDVal) + " not supported yet");
1170 // Look up the handler in the handler table.
1171 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1172 DirectiveMap.lookup(IDVal);
1174 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1176 // Target hook for parsing target specific directives.
1177 if (!getTargetParser().ParseDirective(ID))
1180 bool retval = Warning(IDLoc, "ignoring directive for now");
1181 EatToEndOfStatement();
1185 CheckForValidSection();
1187 // Canonicalize the opcode to lower case.
1188 SmallString<128> Opcode;
1189 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1190 Opcode.push_back(tolower(IDVal[i]));
1192 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
1193 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
1196 // Dump the parsed representation, if requested.
1197 if (getShowParsedOperands()) {
1198 SmallString<256> Str;
1199 raw_svector_ostream OS(Str);
1200 OS << "parsed instruction: [";
1201 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1204 ParsedOperands[i]->print(OS);
1208 PrintMessage(IDLoc, OS.str(), "note");
1211 // If parsing succeeded, match the instruction.
1213 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1216 // Free any parsed operands.
1217 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1218 delete ParsedOperands[i];
1220 // Don't skip the rest of the line, the instruction parser is responsible for
1225 /// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1226 /// since they may not be able to be tokenized to get to the end of line token.
1227 void AsmParser::EatToEndOfLine() {
1228 if (!Lexer.is(AsmToken::EndOfStatement))
1229 Lexer.LexUntilEndOfLine();
1234 /// ParseCppHashLineFilenameComment as this:
1235 /// ::= # number "filename"
1236 /// or just as a full line comment if it doesn't have a number and a string.
1237 bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1238 Lex(); // Eat the hash token.
1240 if (getLexer().isNot(AsmToken::Integer)) {
1241 // Consume the line since in cases it is not a well-formed line directive,
1242 // as if were simply a full line comment.
1247 int64_t LineNumber = getTok().getIntVal();
1250 if (getLexer().isNot(AsmToken::String)) {
1255 StringRef Filename = getTok().getString();
1256 // Get rid of the enclosing quotes.
1257 Filename = Filename.substr(1, Filename.size()-2);
1259 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1261 CppHashFilename = Filename;
1262 CppHashLineNumber = LineNumber;
1264 // Ignore any trailing characters, they're just comment.
1269 /// DiagHandler - will use the the last parsed cpp hash line filename comment
1270 /// for the Filename and LineNo if any in the diagnostic.
1271 void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1272 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1273 raw_ostream &OS = errs();
1275 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1276 const SMLoc &DiagLoc = Diag.getLoc();
1277 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1278 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1280 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1281 // before printing the message.
1282 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1283 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
1284 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1285 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1288 // If we have not parsed a cpp hash line filename comment or the source
1289 // manager changed or buffer changed (like in a nested include) then just
1290 // print the normal diagnostic using its Filename and LineNo.
1291 if (!Parser->CppHashLineNumber ||
1292 &DiagSrcMgr != &Parser->SrcMgr ||
1293 DiagBuf != CppHashBuf) {
1294 if (Parser->SavedDiagHandler)
1295 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1301 // Use the CppHashFilename and calculate a line number based on the
1302 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1304 const std::string Filename = Parser->CppHashFilename;
1306 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1307 int CppHashLocLineNo =
1308 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1309 int LineNo = Parser->CppHashLineNumber - 1 +
1310 (DiagLocLineNo - CppHashLocLineNo);
1312 SMDiagnostic NewDiag(*Diag.getSourceMgr(),
1318 Diag.getLineContents(),
1319 Diag.getShowLine());
1321 if (Parser->SavedDiagHandler)
1322 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1324 NewDiag.Print(0, OS);
1327 bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1328 const std::vector<StringRef> &Parameters,
1329 const std::vector<std::vector<AsmToken> > &A,
1331 raw_svector_ostream OS(Buf);
1332 unsigned NParameters = Parameters.size();
1333 if (NParameters != 0 && NParameters != A.size())
1334 return Error(L, "Wrong number of arguments");
1336 while (!Body.empty()) {
1337 // Scan for the next substitution.
1338 std::size_t End = Body.size(), Pos = 0;
1339 for (; Pos != End; ++Pos) {
1340 // Check for a substitution or escape.
1342 // This macro has no parameters, look for $0, $1, etc.
1343 if (Body[Pos] != '$' || Pos + 1 == End)
1346 char Next = Body[Pos + 1];
1347 if (Next == '$' || Next == 'n' || isdigit(Next))
1350 // This macro has parameters, look for \foo, \bar, etc.
1351 if (Body[Pos] == '\\' && Pos + 1 != End)
1357 OS << Body.slice(0, Pos);
1359 // Check if we reached the end.
1364 switch (Body[Pos+1]) {
1370 // $n => number of arguments
1375 // $[0-9] => argument
1377 // Missing arguments are ignored.
1378 unsigned Index = Body[Pos+1] - '0';
1379 if (Index >= A.size())
1382 // Otherwise substitute with the token values, with spaces eliminated.
1383 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1384 ie = A[Index].end(); it != ie; ++it)
1385 OS << it->getString();
1391 unsigned I = Pos + 1;
1392 while (isalnum(Body[I]) && I + 1 != End)
1395 const char *Begin = Body.data() + Pos +1;
1396 StringRef Argument(Begin, I - (Pos +1));
1398 for (; Index < NParameters; ++Index)
1399 if (Parameters[Index] == Argument)
1402 // FIXME: We should error at the macro definition.
1403 if (Index == NParameters)
1404 return Error(L, "Parameter not found");
1406 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1407 ie = A[Index].end(); it != ie; ++it)
1408 OS << it->getString();
1410 Pos += 1 + Argument.size();
1412 // Update the scan point.
1413 Body = Body.substr(Pos);
1416 // We include the .endmacro in the buffer as our queue to exit the macro
1418 OS << ".endmacro\n";
1422 MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1424 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1428 bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1430 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1431 // this, although we should protect against infinite loops.
1432 if (ActiveMacros.size() == 20)
1433 return TokError("macros cannot be nested more than 20 levels deep");
1435 // Parse the macro instantiation arguments.
1436 std::vector<std::vector<AsmToken> > MacroArguments;
1437 MacroArguments.push_back(std::vector<AsmToken>());
1438 unsigned ParenLevel = 0;
1440 if (Lexer.is(AsmToken::Eof))
1441 return TokError("unexpected token in macro instantiation");
1442 if (Lexer.is(AsmToken::EndOfStatement))
1445 // If we aren't inside parentheses and this is a comma, start a new token
1447 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1448 MacroArguments.push_back(std::vector<AsmToken>());
1450 // Adjust the current parentheses level.
1451 if (Lexer.is(AsmToken::LParen))
1453 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1456 // Append the token to the current argument list.
1457 MacroArguments.back().push_back(getTok());
1462 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1463 // to hold the macro body with substitutions.
1464 SmallString<256> Buf;
1465 StringRef Body = M->Body;
1467 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1470 MemoryBuffer *Instantiation =
1471 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1473 // Create the macro instantiation object and add to the current macro
1474 // instantiation stack.
1475 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
1478 ActiveMacros.push_back(MI);
1480 // Jump to the macro instantiation and prime the lexer.
1481 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1482 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1488 void AsmParser::HandleMacroExit() {
1489 // Jump to the EndOfStatement we should return to, and consume it.
1490 JumpToLoc(ActiveMacros.back()->ExitLoc);
1493 // Pop the instantiation entry.
1494 delete ActiveMacros.back();
1495 ActiveMacros.pop_back();
1498 static void MarkUsed(const MCExpr *Value) {
1499 switch (Value->getKind()) {
1500 case MCExpr::Binary:
1501 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1502 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1504 case MCExpr::Target:
1505 case MCExpr::Constant:
1507 case MCExpr::SymbolRef: {
1508 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1512 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1517 bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
1518 // FIXME: Use better location, we should use proper tokens.
1519 SMLoc EqualLoc = Lexer.getLoc();
1521 const MCExpr *Value;
1522 if (ParseExpression(Value))
1527 if (Lexer.isNot(AsmToken::EndOfStatement))
1528 return TokError("unexpected token in assignment");
1530 // Error on assignment to '.'.
1532 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1533 "(use '.space' or '.org').)"));
1536 // Eat the end of statement marker.
1539 // Validate that the LHS is allowed to be a variable (either it has not been
1540 // used as a symbol, or it is an absolute symbol).
1541 MCSymbol *Sym = getContext().LookupSymbol(Name);
1543 // Diagnose assignment to a label.
1545 // FIXME: Diagnostics. Note the location of the definition as a label.
1546 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
1547 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
1548 ; // Allow redefinitions of undefined symbols only used in directives.
1549 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
1550 return Error(EqualLoc, "redefinition of '" + Name + "'");
1551 else if (!Sym->isVariable())
1552 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
1553 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
1554 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1557 // Don't count these checks as uses.
1558 Sym->setUsed(false);
1560 Sym = getContext().GetOrCreateSymbol(Name);
1562 // FIXME: Handle '.'.
1564 // Do the assignment.
1565 Out.EmitAssignment(Sym, Value);
1570 /// ParseIdentifier:
1573 bool AsmParser::ParseIdentifier(StringRef &Res) {
1574 // The assembler has relaxed rules for accepting identifiers, in particular we
1575 // allow things like '.globl $foo', which would normally be separate
1576 // tokens. At this level, we have already lexed so we cannot (currently)
1577 // handle this as a context dependent token, instead we detect adjacent tokens
1578 // and return the combined identifier.
1579 if (Lexer.is(AsmToken::Dollar)) {
1580 SMLoc DollarLoc = getLexer().getLoc();
1582 // Consume the dollar sign, and check for a following identifier.
1584 if (Lexer.isNot(AsmToken::Identifier))
1587 // We have a '$' followed by an identifier, make sure they are adjacent.
1588 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1591 // Construct the joined identifier and consume the token.
1592 Res = StringRef(DollarLoc.getPointer(),
1593 getTok().getIdentifier().size() + 1);
1598 if (Lexer.isNot(AsmToken::Identifier) &&
1599 Lexer.isNot(AsmToken::String))
1602 Res = getTok().getIdentifier();
1604 Lex(); // Consume the identifier token.
1609 /// ParseDirectiveSet:
1610 /// ::= .equ identifier ',' expression
1611 /// ::= .equiv identifier ',' expression
1612 /// ::= .set identifier ',' expression
1613 bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
1616 if (ParseIdentifier(Name))
1617 return TokError("expected identifier after '" + Twine(IDVal) + "'");
1619 if (getLexer().isNot(AsmToken::Comma))
1620 return TokError("unexpected token in '" + Twine(IDVal) + "'");
1623 return ParseAssignment(Name, allow_redef);
1626 bool AsmParser::ParseEscapedString(std::string &Data) {
1627 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
1630 StringRef Str = getTok().getStringContents();
1631 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1632 if (Str[i] != '\\') {
1637 // Recognize escaped characters. Note that this escape semantics currently
1638 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1641 return TokError("unexpected backslash at end of string");
1643 // Recognize octal sequences.
1644 if ((unsigned) (Str[i] - '0') <= 7) {
1645 // Consume up to three octal characters.
1646 unsigned Value = Str[i] - '0';
1648 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1650 Value = Value * 8 + (Str[i] - '0');
1652 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1654 Value = Value * 8 + (Str[i] - '0');
1659 return TokError("invalid octal escape sequence (out of range)");
1661 Data += (unsigned char) Value;
1665 // Otherwise recognize individual escapes.
1668 // Just reject invalid escape sequences for now.
1669 return TokError("invalid escape sequence (unrecognized character)");
1671 case 'b': Data += '\b'; break;
1672 case 'f': Data += '\f'; break;
1673 case 'n': Data += '\n'; break;
1674 case 'r': Data += '\r'; break;
1675 case 't': Data += '\t'; break;
1676 case '"': Data += '"'; break;
1677 case '\\': Data += '\\'; break;
1684 /// ParseDirectiveAscii:
1685 /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1686 bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
1687 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1688 CheckForValidSection();
1691 if (getLexer().isNot(AsmToken::String))
1692 return TokError("expected string in '" + Twine(IDVal) + "' directive");
1695 if (ParseEscapedString(Data))
1698 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
1700 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1704 if (getLexer().is(AsmToken::EndOfStatement))
1707 if (getLexer().isNot(AsmToken::Comma))
1708 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
1717 /// ParseDirectiveValue
1718 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1719 bool AsmParser::ParseDirectiveValue(unsigned Size) {
1720 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1721 CheckForValidSection();
1724 const MCExpr *Value;
1725 SMLoc ExprLoc = getLexer().getLoc();
1726 if (ParseExpression(Value))
1729 // Special case constant expressions to match code generator.
1730 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1731 assert(Size <= 8 && "Invalid size");
1732 uint64_t IntValue = MCE->getValue();
1733 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1734 return Error(ExprLoc, "literal value out of range for directive");
1735 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1737 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
1739 if (getLexer().is(AsmToken::EndOfStatement))
1742 // FIXME: Improve diagnostic.
1743 if (getLexer().isNot(AsmToken::Comma))
1744 return TokError("unexpected token in directive");
1753 /// ParseDirectiveRealValue
1754 /// ::= (.single | .double) [ expression (, expression)* ]
1755 bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1756 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1757 CheckForValidSection();
1760 // We don't truly support arithmetic on floating point expressions, so we
1761 // have to manually parse unary prefixes.
1763 if (getLexer().is(AsmToken::Minus)) {
1766 } else if (getLexer().is(AsmToken::Plus))
1769 if (getLexer().isNot(AsmToken::Integer) &&
1770 getLexer().isNot(AsmToken::Real) &&
1771 getLexer().isNot(AsmToken::Identifier))
1772 return TokError("unexpected token in directive");
1774 // Convert to an APFloat.
1775 APFloat Value(Semantics);
1776 StringRef IDVal = getTok().getString();
1777 if (getLexer().is(AsmToken::Identifier)) {
1778 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1779 Value = APFloat::getInf(Semantics);
1780 else if (!IDVal.compare_lower("nan"))
1781 Value = APFloat::getNaN(Semantics, false, ~0);
1783 return TokError("invalid floating point literal");
1784 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
1785 APFloat::opInvalidOp)
1786 return TokError("invalid floating point literal");
1790 // Consume the numeric token.
1793 // Emit the value as an integer.
1794 APInt AsInt = Value.bitcastToAPInt();
1795 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1796 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1798 if (getLexer().is(AsmToken::EndOfStatement))
1801 if (getLexer().isNot(AsmToken::Comma))
1802 return TokError("unexpected token in directive");
1811 /// ParseDirectiveSpace
1812 /// ::= .space expression [ , expression ]
1813 bool AsmParser::ParseDirectiveSpace() {
1814 CheckForValidSection();
1817 if (ParseAbsoluteExpression(NumBytes))
1820 int64_t FillExpr = 0;
1821 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1822 if (getLexer().isNot(AsmToken::Comma))
1823 return TokError("unexpected token in '.space' directive");
1826 if (ParseAbsoluteExpression(FillExpr))
1829 if (getLexer().isNot(AsmToken::EndOfStatement))
1830 return TokError("unexpected token in '.space' directive");
1836 return TokError("invalid number of bytes in '.space' directive");
1838 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1839 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
1844 /// ParseDirectiveZero
1845 /// ::= .zero expression
1846 bool AsmParser::ParseDirectiveZero() {
1847 CheckForValidSection();
1850 if (ParseAbsoluteExpression(NumBytes))
1854 if (getLexer().is(AsmToken::Comma)) {
1856 if (ParseAbsoluteExpression(Val))
1860 if (getLexer().isNot(AsmToken::EndOfStatement))
1861 return TokError("unexpected token in '.zero' directive");
1865 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
1870 /// ParseDirectiveFill
1871 /// ::= .fill expression , expression , expression
1872 bool AsmParser::ParseDirectiveFill() {
1873 CheckForValidSection();
1876 if (ParseAbsoluteExpression(NumValues))
1879 if (getLexer().isNot(AsmToken::Comma))
1880 return TokError("unexpected token in '.fill' directive");
1884 if (ParseAbsoluteExpression(FillSize))
1887 if (getLexer().isNot(AsmToken::Comma))
1888 return TokError("unexpected token in '.fill' directive");
1892 if (ParseAbsoluteExpression(FillExpr))
1895 if (getLexer().isNot(AsmToken::EndOfStatement))
1896 return TokError("unexpected token in '.fill' directive");
1900 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1901 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1903 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1904 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
1909 /// ParseDirectiveOrg
1910 /// ::= .org expression [ , expression ]
1911 bool AsmParser::ParseDirectiveOrg() {
1912 CheckForValidSection();
1914 const MCExpr *Offset;
1915 if (ParseExpression(Offset))
1918 // Parse optional fill expression.
1919 int64_t FillExpr = 0;
1920 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1921 if (getLexer().isNot(AsmToken::Comma))
1922 return TokError("unexpected token in '.org' directive");
1925 if (ParseAbsoluteExpression(FillExpr))
1928 if (getLexer().isNot(AsmToken::EndOfStatement))
1929 return TokError("unexpected token in '.org' directive");
1934 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1935 // has to be relative to the current section.
1936 getStreamer().EmitValueToOffset(Offset, FillExpr);
1941 /// ParseDirectiveAlign
1942 /// ::= {.align, ...} expression [ , expression [ , expression ]]
1943 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1944 CheckForValidSection();
1946 SMLoc AlignmentLoc = getLexer().getLoc();
1948 if (ParseAbsoluteExpression(Alignment))
1952 bool HasFillExpr = false;
1953 int64_t FillExpr = 0;
1954 int64_t MaxBytesToFill = 0;
1955 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1956 if (getLexer().isNot(AsmToken::Comma))
1957 return TokError("unexpected token in directive");
1960 // The fill expression can be omitted while specifying a maximum number of
1961 // alignment bytes, e.g:
1963 if (getLexer().isNot(AsmToken::Comma)) {
1965 if (ParseAbsoluteExpression(FillExpr))
1969 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1970 if (getLexer().isNot(AsmToken::Comma))
1971 return TokError("unexpected token in directive");
1974 MaxBytesLoc = getLexer().getLoc();
1975 if (ParseAbsoluteExpression(MaxBytesToFill))
1978 if (getLexer().isNot(AsmToken::EndOfStatement))
1979 return TokError("unexpected token in directive");
1988 // Compute alignment in bytes.
1990 // FIXME: Diagnose overflow.
1991 if (Alignment >= 32) {
1992 Error(AlignmentLoc, "invalid alignment value");
1996 Alignment = 1ULL << Alignment;
1999 // Diagnose non-sensical max bytes to align.
2000 if (MaxBytesLoc.isValid()) {
2001 if (MaxBytesToFill < 1) {
2002 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2003 "many bytes, ignoring maximum bytes expression");
2007 if (MaxBytesToFill >= Alignment) {
2008 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2014 // Check whether we should use optimal code alignment for this .align
2016 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
2017 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2018 ValueSize == 1 && UseCodeAlign) {
2019 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
2021 // FIXME: Target specific behavior about how the "extra" bytes are filled.
2022 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2029 /// ParseDirectiveSymbolAttribute
2030 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
2031 bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
2032 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2035 SMLoc Loc = getTok().getLoc();
2037 if (ParseIdentifier(Name))
2038 return Error(Loc, "expected identifier in directive");
2040 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2042 // Assembler local symbols don't make any sense here. Complain loudly.
2043 if (Sym->isTemporary())
2044 return Error(Loc, "non-local symbol required in directive");
2046 getStreamer().EmitSymbolAttribute(Sym, Attr);
2048 if (getLexer().is(AsmToken::EndOfStatement))
2051 if (getLexer().isNot(AsmToken::Comma))
2052 return TokError("unexpected token in directive");
2061 /// ParseDirectiveComm
2062 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2063 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
2064 CheckForValidSection();
2066 SMLoc IDLoc = getLexer().getLoc();
2068 if (ParseIdentifier(Name))
2069 return TokError("expected identifier in directive");
2071 // Handle the identifier as the key symbol.
2072 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2074 if (getLexer().isNot(AsmToken::Comma))
2075 return TokError("unexpected token in directive");
2079 SMLoc SizeLoc = getLexer().getLoc();
2080 if (ParseAbsoluteExpression(Size))
2083 int64_t Pow2Alignment = 0;
2084 SMLoc Pow2AlignmentLoc;
2085 if (getLexer().is(AsmToken::Comma)) {
2087 Pow2AlignmentLoc = getLexer().getLoc();
2088 if (ParseAbsoluteExpression(Pow2Alignment))
2091 // If this target takes alignments in bytes (not log) validate and convert.
2092 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2093 if (!isPowerOf2_64(Pow2Alignment))
2094 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2095 Pow2Alignment = Log2_64(Pow2Alignment);
2099 if (getLexer().isNot(AsmToken::EndOfStatement))
2100 return TokError("unexpected token in '.comm' or '.lcomm' directive");
2104 // NOTE: a size of zero for a .comm should create a undefined symbol
2105 // but a size of .lcomm creates a bss symbol of size zero.
2107 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2108 "be less than zero");
2110 // NOTE: The alignment in the directive is a power of 2 value, the assembler
2111 // may internally end up wanting an alignment in bytes.
2112 // FIXME: Diagnose overflow.
2113 if (Pow2Alignment < 0)
2114 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2115 "alignment, can't be less than zero");
2117 if (!Sym->isUndefined())
2118 return Error(IDLoc, "invalid symbol redefinition");
2120 // '.lcomm' is equivalent to '.zerofill'.
2121 // Create the Symbol as a common or local common with Size and Pow2Alignment
2123 getStreamer().EmitZerofill(Ctx.getMachOSection(
2124 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2125 0, SectionKind::getBSS()),
2126 Sym, Size, 1 << Pow2Alignment);
2130 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
2134 /// ParseDirectiveAbort
2135 /// ::= .abort [... message ...]
2136 bool AsmParser::ParseDirectiveAbort() {
2137 // FIXME: Use loc from directive.
2138 SMLoc Loc = getLexer().getLoc();
2140 StringRef Str = ParseStringToEndOfStatement();
2141 if (getLexer().isNot(AsmToken::EndOfStatement))
2142 return TokError("unexpected token in '.abort' directive");
2147 Error(Loc, ".abort detected. Assembly stopping.");
2149 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
2150 // FIXME: Actually abort assembly here.
2155 /// ParseDirectiveInclude
2156 /// ::= .include "filename"
2157 bool AsmParser::ParseDirectiveInclude() {
2158 if (getLexer().isNot(AsmToken::String))
2159 return TokError("expected string in '.include' directive");
2161 std::string Filename = getTok().getString();
2162 SMLoc IncludeLoc = getLexer().getLoc();
2165 if (getLexer().isNot(AsmToken::EndOfStatement))
2166 return TokError("unexpected token in '.include' directive");
2168 // Strip the quotes.
2169 Filename = Filename.substr(1, Filename.size()-2);
2171 // Attempt to switch the lexer to the included file before consuming the end
2172 // of statement to avoid losing it when we switch.
2173 if (EnterIncludeFile(Filename)) {
2174 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
2181 /// ParseDirectiveIf
2182 /// ::= .if expression
2183 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
2184 TheCondStack.push_back(TheCondState);
2185 TheCondState.TheCond = AsmCond::IfCond;
2186 if(TheCondState.Ignore) {
2187 EatToEndOfStatement();
2191 if (ParseAbsoluteExpression(ExprValue))
2194 if (getLexer().isNot(AsmToken::EndOfStatement))
2195 return TokError("unexpected token in '.if' directive");
2199 TheCondState.CondMet = ExprValue;
2200 TheCondState.Ignore = !TheCondState.CondMet;
2206 bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2208 TheCondStack.push_back(TheCondState);
2209 TheCondState.TheCond = AsmCond::IfCond;
2211 if (TheCondState.Ignore) {
2212 EatToEndOfStatement();
2214 if (ParseIdentifier(Name))
2215 return TokError("expected identifier after '.ifdef'");
2219 MCSymbol *Sym = getContext().LookupSymbol(Name);
2222 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2224 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2225 TheCondState.Ignore = !TheCondState.CondMet;
2231 /// ParseDirectiveElseIf
2232 /// ::= .elseif expression
2233 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2234 if (TheCondState.TheCond != AsmCond::IfCond &&
2235 TheCondState.TheCond != AsmCond::ElseIfCond)
2236 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2238 TheCondState.TheCond = AsmCond::ElseIfCond;
2240 bool LastIgnoreState = false;
2241 if (!TheCondStack.empty())
2242 LastIgnoreState = TheCondStack.back().Ignore;
2243 if (LastIgnoreState || TheCondState.CondMet) {
2244 TheCondState.Ignore = true;
2245 EatToEndOfStatement();
2249 if (ParseAbsoluteExpression(ExprValue))
2252 if (getLexer().isNot(AsmToken::EndOfStatement))
2253 return TokError("unexpected token in '.elseif' directive");
2256 TheCondState.CondMet = ExprValue;
2257 TheCondState.Ignore = !TheCondState.CondMet;
2263 /// ParseDirectiveElse
2265 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
2266 if (getLexer().isNot(AsmToken::EndOfStatement))
2267 return TokError("unexpected token in '.else' directive");
2271 if (TheCondState.TheCond != AsmCond::IfCond &&
2272 TheCondState.TheCond != AsmCond::ElseIfCond)
2273 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2275 TheCondState.TheCond = AsmCond::ElseCond;
2276 bool LastIgnoreState = false;
2277 if (!TheCondStack.empty())
2278 LastIgnoreState = TheCondStack.back().Ignore;
2279 if (LastIgnoreState || TheCondState.CondMet)
2280 TheCondState.Ignore = true;
2282 TheCondState.Ignore = false;
2287 /// ParseDirectiveEndIf
2289 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
2290 if (getLexer().isNot(AsmToken::EndOfStatement))
2291 return TokError("unexpected token in '.endif' directive");
2295 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2296 TheCondStack.empty())
2297 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2299 if (!TheCondStack.empty()) {
2300 TheCondState = TheCondStack.back();
2301 TheCondStack.pop_back();
2307 /// ParseDirectiveFile
2308 /// ::= .file [number] string
2309 bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
2310 // FIXME: I'm not sure what this is.
2311 int64_t FileNumber = -1;
2312 SMLoc FileNumberLoc = getLexer().getLoc();
2313 if (getLexer().is(AsmToken::Integer)) {
2314 FileNumber = getTok().getIntVal();
2318 return TokError("file number less than one");
2321 if (getLexer().isNot(AsmToken::String))
2322 return TokError("unexpected token in '.file' directive");
2324 StringRef Filename = getTok().getString();
2325 Filename = Filename.substr(1, Filename.size()-2);
2328 if (getLexer().isNot(AsmToken::EndOfStatement))
2329 return TokError("unexpected token in '.file' directive");
2331 if (FileNumber == -1)
2332 getStreamer().EmitFileDirective(Filename);
2334 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
2335 Error(FileNumberLoc, "file number already allocated");
2341 /// ParseDirectiveLine
2342 /// ::= .line [number]
2343 bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
2344 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2345 if (getLexer().isNot(AsmToken::Integer))
2346 return TokError("unexpected token in '.line' directive");
2348 int64_t LineNumber = getTok().getIntVal();
2352 // FIXME: Do something with the .line.
2355 if (getLexer().isNot(AsmToken::EndOfStatement))
2356 return TokError("unexpected token in '.line' directive");
2362 /// ParseDirectiveLoc
2363 /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2364 /// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2365 /// The first number is a file number, must have been previously assigned with
2366 /// a .file directive, the second number is the line number and optionally the
2367 /// third number is a column position (zero if not specified). The remaining
2368 /// optional items are .loc sub-directives.
2369 bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
2371 if (getLexer().isNot(AsmToken::Integer))
2372 return TokError("unexpected token in '.loc' directive");
2373 int64_t FileNumber = getTok().getIntVal();
2375 return TokError("file number less than one in '.loc' directive");
2376 if (!getContext().isValidDwarfFileNumber(FileNumber))
2377 return TokError("unassigned file number in '.loc' directive");
2380 int64_t LineNumber = 0;
2381 if (getLexer().is(AsmToken::Integer)) {
2382 LineNumber = getTok().getIntVal();
2384 return TokError("line number less than one in '.loc' directive");
2388 int64_t ColumnPos = 0;
2389 if (getLexer().is(AsmToken::Integer)) {
2390 ColumnPos = getTok().getIntVal();
2392 return TokError("column position less than zero in '.loc' directive");
2396 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2398 int64_t Discriminator = 0;
2399 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2401 if (getLexer().is(AsmToken::EndOfStatement))
2405 SMLoc Loc = getTok().getLoc();
2406 if (getParser().ParseIdentifier(Name))
2407 return TokError("unexpected token in '.loc' directive");
2409 if (Name == "basic_block")
2410 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2411 else if (Name == "prologue_end")
2412 Flags |= DWARF2_FLAG_PROLOGUE_END;
2413 else if (Name == "epilogue_begin")
2414 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2415 else if (Name == "is_stmt") {
2416 SMLoc Loc = getTok().getLoc();
2417 const MCExpr *Value;
2418 if (getParser().ParseExpression(Value))
2420 // The expression must be the constant 0 or 1.
2421 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2422 int Value = MCE->getValue();
2424 Flags &= ~DWARF2_FLAG_IS_STMT;
2425 else if (Value == 1)
2426 Flags |= DWARF2_FLAG_IS_STMT;
2428 return Error(Loc, "is_stmt value not 0 or 1");
2431 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2434 else if (Name == "isa") {
2435 SMLoc Loc = getTok().getLoc();
2436 const MCExpr *Value;
2437 if (getParser().ParseExpression(Value))
2439 // The expression must be a constant greater or equal to 0.
2440 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2441 int Value = MCE->getValue();
2443 return Error(Loc, "isa number less than zero");
2447 return Error(Loc, "isa number not a constant value");
2450 else if (Name == "discriminator") {
2451 if (getParser().ParseAbsoluteExpression(Discriminator))
2455 return Error(Loc, "unknown sub-directive in '.loc' directive");
2458 if (getLexer().is(AsmToken::EndOfStatement))
2463 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2464 Isa, Discriminator, StringRef());
2469 /// ParseDirectiveStabs
2470 /// ::= .stabs string, number, number, number
2471 bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2472 SMLoc DirectiveLoc) {
2473 return TokError("unsupported directive '" + Directive + "'");
2476 /// ParseDirectiveCFISections
2477 /// ::= .cfi_sections section [, section]
2478 bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2479 SMLoc DirectiveLoc) {
2484 if (getParser().ParseIdentifier(Name))
2485 return TokError("Expected an identifier");
2487 if (Name == ".eh_frame")
2489 else if (Name == ".debug_frame")
2492 if (getLexer().is(AsmToken::Comma)) {
2495 if (getParser().ParseIdentifier(Name))
2496 return TokError("Expected an identifier");
2498 if (Name == ".eh_frame")
2500 else if (Name == ".debug_frame")
2504 getStreamer().EmitCFISections(EH, Debug);
2509 /// ParseDirectiveCFIStartProc
2510 /// ::= .cfi_startproc
2511 bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2512 SMLoc DirectiveLoc) {
2513 getStreamer().EmitCFIStartProc();
2517 /// ParseDirectiveCFIEndProc
2518 /// ::= .cfi_endproc
2519 bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
2520 getStreamer().EmitCFIEndProc();
2524 /// ParseRegisterOrRegisterNumber - parse register name or number.
2525 bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2526 SMLoc DirectiveLoc) {
2529 if (getLexer().isNot(AsmToken::Integer)) {
2530 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2533 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
2535 return getParser().ParseAbsoluteExpression(Register);
2540 /// ParseDirectiveCFIDefCfa
2541 /// ::= .cfi_def_cfa register, offset
2542 bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2543 SMLoc DirectiveLoc) {
2544 int64_t Register = 0;
2545 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2548 if (getLexer().isNot(AsmToken::Comma))
2549 return TokError("unexpected token in directive");
2553 if (getParser().ParseAbsoluteExpression(Offset))
2556 getStreamer().EmitCFIDefCfa(Register, Offset);
2560 /// ParseDirectiveCFIDefCfaOffset
2561 /// ::= .cfi_def_cfa_offset offset
2562 bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2563 SMLoc DirectiveLoc) {
2565 if (getParser().ParseAbsoluteExpression(Offset))
2568 getStreamer().EmitCFIDefCfaOffset(Offset);
2572 /// ParseDirectiveCFIAdjustCfaOffset
2573 /// ::= .cfi_adjust_cfa_offset adjustment
2574 bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2575 SMLoc DirectiveLoc) {
2576 int64_t Adjustment = 0;
2577 if (getParser().ParseAbsoluteExpression(Adjustment))
2580 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2584 /// ParseDirectiveCFIDefCfaRegister
2585 /// ::= .cfi_def_cfa_register register
2586 bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2587 SMLoc DirectiveLoc) {
2588 int64_t Register = 0;
2589 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2592 getStreamer().EmitCFIDefCfaRegister(Register);
2596 /// ParseDirectiveCFIOffset
2597 /// ::= .cfi_offset register, offset
2598 bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2599 int64_t Register = 0;
2602 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2605 if (getLexer().isNot(AsmToken::Comma))
2606 return TokError("unexpected token in directive");
2609 if (getParser().ParseAbsoluteExpression(Offset))
2612 getStreamer().EmitCFIOffset(Register, Offset);
2616 /// ParseDirectiveCFIRelOffset
2617 /// ::= .cfi_rel_offset register, offset
2618 bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2619 SMLoc DirectiveLoc) {
2620 int64_t Register = 0;
2622 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2625 if (getLexer().isNot(AsmToken::Comma))
2626 return TokError("unexpected token in directive");
2630 if (getParser().ParseAbsoluteExpression(Offset))
2633 getStreamer().EmitCFIRelOffset(Register, Offset);
2637 static bool isValidEncoding(int64_t Encoding) {
2638 if (Encoding & ~0xff)
2641 if (Encoding == dwarf::DW_EH_PE_omit)
2644 const unsigned Format = Encoding & 0xf;
2645 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2646 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2647 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2648 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2651 const unsigned Application = Encoding & 0x70;
2652 if (Application != dwarf::DW_EH_PE_absptr &&
2653 Application != dwarf::DW_EH_PE_pcrel)
2659 /// ParseDirectiveCFIPersonalityOrLsda
2660 /// ::= .cfi_personality encoding, [symbol_name]
2661 /// ::= .cfi_lsda encoding, [symbol_name]
2662 bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
2663 SMLoc DirectiveLoc) {
2664 int64_t Encoding = 0;
2665 if (getParser().ParseAbsoluteExpression(Encoding))
2667 if (Encoding == dwarf::DW_EH_PE_omit)
2670 if (!isValidEncoding(Encoding))
2671 return TokError("unsupported encoding.");
2673 if (getLexer().isNot(AsmToken::Comma))
2674 return TokError("unexpected token in directive");
2678 if (getParser().ParseIdentifier(Name))
2679 return TokError("expected identifier in directive");
2681 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2683 if (IDVal == ".cfi_personality")
2684 getStreamer().EmitCFIPersonality(Sym, Encoding);
2686 assert(IDVal == ".cfi_lsda");
2687 getStreamer().EmitCFILsda(Sym, Encoding);
2692 /// ParseDirectiveCFIRememberState
2693 /// ::= .cfi_remember_state
2694 bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2695 SMLoc DirectiveLoc) {
2696 getStreamer().EmitCFIRememberState();
2700 /// ParseDirectiveCFIRestoreState
2701 /// ::= .cfi_remember_state
2702 bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2703 SMLoc DirectiveLoc) {
2704 getStreamer().EmitCFIRestoreState();
2708 /// ParseDirectiveCFISameValue
2709 /// ::= .cfi_same_value register
2710 bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2711 SMLoc DirectiveLoc) {
2712 int64_t Register = 0;
2714 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2717 getStreamer().EmitCFISameValue(Register);
2722 /// ParseDirectiveMacrosOnOff
2725 bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2726 SMLoc DirectiveLoc) {
2727 if (getLexer().isNot(AsmToken::EndOfStatement))
2728 return Error(getLexer().getLoc(),
2729 "unexpected token in '" + Directive + "' directive");
2731 getParser().MacrosEnabled = Directive == ".macros_on";
2736 /// ParseDirectiveMacro
2737 /// ::= .macro name [parameters]
2738 bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2739 SMLoc DirectiveLoc) {
2741 if (getParser().ParseIdentifier(Name))
2742 return TokError("expected identifier in directive");
2744 std::vector<StringRef> Parameters;
2745 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2747 StringRef Parameter;
2748 if (getParser().ParseIdentifier(Parameter))
2749 return TokError("expected identifier in directive");
2750 Parameters.push_back(Parameter);
2752 if (getLexer().isNot(AsmToken::Comma))
2758 if (getLexer().isNot(AsmToken::EndOfStatement))
2759 return TokError("unexpected token in '.macro' directive");
2761 // Eat the end of statement.
2764 AsmToken EndToken, StartToken = getTok();
2766 // Lex the macro definition.
2768 // Check whether we have reached the end of the file.
2769 if (getLexer().is(AsmToken::Eof))
2770 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2772 // Otherwise, check whether we have reach the .endmacro.
2773 if (getLexer().is(AsmToken::Identifier) &&
2774 (getTok().getIdentifier() == ".endm" ||
2775 getTok().getIdentifier() == ".endmacro")) {
2776 EndToken = getTok();
2778 if (getLexer().isNot(AsmToken::EndOfStatement))
2779 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2784 // Otherwise, scan til the end of the statement.
2785 getParser().EatToEndOfStatement();
2788 if (getParser().MacroMap.lookup(Name)) {
2789 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2792 const char *BodyStart = StartToken.getLoc().getPointer();
2793 const char *BodyEnd = EndToken.getLoc().getPointer();
2794 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2795 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
2799 /// ParseDirectiveEndMacro
2802 bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2803 SMLoc DirectiveLoc) {
2804 if (getLexer().isNot(AsmToken::EndOfStatement))
2805 return TokError("unexpected token in '" + Directive + "' directive");
2807 // If we are inside a macro instantiation, terminate the current
2809 if (!getParser().ActiveMacros.empty()) {
2810 getParser().HandleMacroExit();
2814 // Otherwise, this .endmacro is a stray entry in the file; well formed
2815 // .endmacro directives are handled during the macro definition parsing.
2816 return TokError("unexpected '" + Directive + "' in file, "
2817 "no current macro definition");
2820 bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
2821 getParser().CheckForValidSection();
2823 const MCExpr *Value;
2825 if (getParser().ParseExpression(Value))
2828 if (getLexer().isNot(AsmToken::EndOfStatement))
2829 return TokError("unexpected token in directive");
2831 if (DirName[1] == 's')
2832 getStreamer().EmitSLEB128Value(Value);
2834 getStreamer().EmitULEB128Value(Value);
2840 /// \brief Create an MCAsmParser instance.
2841 MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
2842 MCContext &C, MCStreamer &Out,
2843 const MCAsmInfo &MAI) {
2844 return new AsmParser(SM, C, Out, MAI);