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/Twine.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCDwarf.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCParser/AsmCond.h"
23 #include "llvm/MC/MCParser/AsmLexer.h"
24 #include "llvm/MC/MCParser/MCAsmParser.h"
25 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26 #include "llvm/MC/MCRegisterInfo.h"
27 #include "llvm/MC/MCSectionMachO.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/MC/MCSymbol.h"
30 #include "llvm/MC/MCTargetAsmParser.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/ErrorHandling.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.
48 typedef std::vector<AsmToken> MacroArgument;
49 typedef std::vector<MacroArgument> MacroArguments;
50 typedef StringRef MacroParameter;
51 typedef std::vector<MacroParameter> MacroParameters;
56 MacroParameters Parameters;
59 Macro(StringRef N, StringRef B, const MacroParameters &P) :
60 Name(N), Body(B), Parameters(P) {}
63 /// \brief Helper class for storing information about an active macro
65 struct MacroInstantiation {
66 /// The macro being instantiated.
67 const Macro *TheMacro;
69 /// The macro instantiation with substitutions.
70 MemoryBuffer *Instantiation;
72 /// The location of the instantiation.
73 SMLoc InstantiationLoc;
75 /// The location where parsing should resume upon instantiation completion.
79 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
83 /// \brief The concrete assembly parser instance.
84 class AsmParser : public MCAsmParser {
85 friend class GenericAsmParser;
87 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
88 void operator=(const AsmParser &); // DO NOT IMPLEMENT
95 SourceMgr::DiagHandlerTy SavedDiagHandler;
96 void *SavedDiagContext;
97 MCAsmParserExtension *GenericParser;
98 MCAsmParserExtension *PlatformParser;
100 /// This is the current buffer index we're lexing from as managed by the
101 /// SourceMgr object.
104 AsmCond TheCondState;
105 std::vector<AsmCond> TheCondStack;
107 /// DirectiveMap - This is a table handlers for directives. Each handler is
108 /// invoked after the directive identifier is read and is responsible for
109 /// parsing and validating the rest of the directive. The handler is passed
110 /// in the directive name and the location of the directive keyword.
111 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
113 /// MacroMap - Map of currently defined macros.
114 StringMap<Macro*> MacroMap;
116 /// ActiveMacros - Stack of active macro instantiations.
117 std::vector<MacroInstantiation*> ActiveMacros;
119 /// Boolean tracking whether macro substitution is enabled.
120 unsigned MacrosEnabled : 1;
122 /// Flag tracking whether any errors have been encountered.
123 unsigned HadError : 1;
125 /// The values from the last parsed cpp hash file line comment if any.
126 StringRef CppHashFilename;
127 int64_t CppHashLineNumber;
130 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
131 unsigned AssemblerDialect;
134 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
135 const MCAsmInfo &MAI);
136 virtual ~AsmParser();
138 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
140 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
142 DirectiveHandler Handler) {
143 DirectiveMap[Directive] = std::make_pair(Object, Handler);
147 /// @name MCAsmParser Interface
150 virtual SourceMgr &getSourceManager() { return SrcMgr; }
151 virtual MCAsmLexer &getLexer() { return Lexer; }
152 virtual MCContext &getContext() { return Ctx; }
153 virtual MCStreamer &getStreamer() { return Out; }
154 virtual unsigned getAssemblerDialect() {
155 if (AssemblerDialect == ~0U)
156 return MAI.getAssemblerDialect();
158 return AssemblerDialect;
160 virtual void setAssemblerDialect(unsigned i) {
161 AssemblerDialect = i;
164 virtual bool Warning(SMLoc L, const Twine &Msg,
165 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
166 virtual bool Error(SMLoc L, const Twine &Msg,
167 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
169 virtual const AsmToken &Lex();
171 bool ParseExpression(const MCExpr *&Res);
172 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
173 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
174 virtual bool ParseAbsoluteExpression(int64_t &Res);
179 void CheckForValidSection();
181 bool ParseStatement();
182 void EatToEndOfLine();
183 bool ParseCppHashLineFilenameComment(const SMLoc &L);
185 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
186 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
187 const MacroParameters &Parameters,
188 const MacroArguments &A,
190 void HandleMacroExit();
192 void PrintMacroInstantiations();
193 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
194 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
195 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
197 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
199 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
200 bool EnterIncludeFile(const std::string &Filename);
201 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
202 /// This returns true on failure.
203 bool ProcessIncbinFile(const std::string &Filename);
205 /// \brief Reset the current lexer position to that given by \arg Loc. The
206 /// current token is not set; clients should ensure Lex() is called
208 void JumpToLoc(SMLoc Loc);
210 virtual void EatToEndOfStatement();
212 bool ParseMacroArgument(MacroArgument &MA);
213 bool ParseMacroArguments(const Macro *M, MacroArguments &A);
215 /// \brief Parse up to the end of statement and a return the contents from the
216 /// current token until the end of the statement; the current token on exit
217 /// will be either the EndOfStatement or EOF.
218 virtual StringRef ParseStringToEndOfStatement();
220 /// \brief Parse until the end of a statement or a comma is encountered,
221 /// return the contents from the current token up to the end or comma.
222 StringRef ParseStringToComma();
224 bool ParseAssignment(StringRef Name, bool allow_redef,
225 bool NoDeadStrip = false);
227 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
228 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
229 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
230 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
232 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
233 /// and set \arg Res to the identifier contents.
234 virtual bool ParseIdentifier(StringRef &Res);
236 // Directive Parsing.
238 // ".ascii", ".asciiz", ".string"
239 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
240 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
241 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
242 bool ParseDirectiveFill(); // ".fill"
243 bool ParseDirectiveSpace(); // ".space"
244 bool ParseDirectiveZero(); // ".zero"
245 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
246 bool ParseDirectiveOrg(); // ".org"
247 // ".align{,32}", ".p2align{,w,l}"
248 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
250 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
251 /// accepts a single symbol (which should be a label or an external).
252 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
254 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
256 bool ParseDirectiveAbort(); // ".abort"
257 bool ParseDirectiveInclude(); // ".include"
258 bool ParseDirectiveIncbin(); // ".incbin"
260 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
261 // ".ifb" or ".ifnb", depending on ExpectBlank.
262 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
263 // ".ifc" or ".ifnc", depending on ExpectEqual.
264 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
265 // ".ifdef" or ".ifndef", depending on expect_defined
266 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
267 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
268 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
269 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
271 /// ParseEscapedString - Parse the current token as a string which may include
272 /// escaped characters and return the string contents.
273 bool ParseEscapedString(std::string &Data);
275 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
276 MCSymbolRefExpr::VariantKind Variant);
278 // Macro-like directives
279 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
280 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
281 raw_svector_ostream &OS);
282 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
283 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
284 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
285 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
288 /// \brief Generic implementations of directive handling, etc. which is shared
289 /// (or the default, at least) for all assembler parser.
290 class GenericAsmParser : public MCAsmParserExtension {
291 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
292 void AddDirectiveHandler(StringRef Directive) {
293 getParser().AddDirectiveHandler(this, Directive,
294 HandleDirective<GenericAsmParser, Handler>);
297 GenericAsmParser() {}
299 AsmParser &getParser() {
300 return (AsmParser&) this->MCAsmParserExtension::getParser();
303 virtual void Initialize(MCAsmParser &Parser) {
304 // Call the base implementation.
305 this->MCAsmParserExtension::Initialize(Parser);
307 // Debugging directives.
308 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
309 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
310 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
311 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
314 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
316 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
318 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
320 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
322 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
323 ".cfi_def_cfa_offset");
324 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
325 ".cfi_adjust_cfa_offset");
326 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
327 ".cfi_def_cfa_register");
328 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
330 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
333 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
335 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
337 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
339 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
341 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
343 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
345 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
347 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
350 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
352 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
354 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
355 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
356 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
357 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
359 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
360 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
363 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
365 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
366 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
367 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
368 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
369 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
370 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
371 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
372 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
373 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
374 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
375 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
376 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
377 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
378 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
379 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
380 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
381 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
382 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
383 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
384 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
386 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
387 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
388 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
389 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
391 bool ParseDirectiveLEB128(StringRef, SMLoc);
398 extern MCAsmParserExtension *createDarwinAsmParser();
399 extern MCAsmParserExtension *createELFAsmParser();
400 extern MCAsmParserExtension *createCOFFAsmParser();
404 enum { DEFAULT_ADDRSPACE = 0 };
406 AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
407 MCStreamer &_Out, const MCAsmInfo &_MAI)
408 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
409 GenericParser(new GenericAsmParser), PlatformParser(0),
410 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
411 AssemblerDialect(~0U) {
412 // Save the old handler.
413 SavedDiagHandler = SrcMgr.getDiagHandler();
414 SavedDiagContext = SrcMgr.getDiagContext();
415 // Set our own handler which calls the saved handler.
416 SrcMgr.setDiagHandler(DiagHandler, this);
417 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
419 // Initialize the generic parser.
420 GenericParser->Initialize(*this);
422 // Initialize the platform / file format parser.
424 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
426 if (_MAI.hasMicrosoftFastStdCallMangling()) {
427 PlatformParser = createCOFFAsmParser();
428 PlatformParser->Initialize(*this);
429 } else if (_MAI.hasSubsectionsViaSymbols()) {
430 PlatformParser = createDarwinAsmParser();
431 PlatformParser->Initialize(*this);
433 PlatformParser = createELFAsmParser();
434 PlatformParser->Initialize(*this);
438 AsmParser::~AsmParser() {
439 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
441 // Destroy any macros.
442 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
443 ie = MacroMap.end(); it != ie; ++it)
444 delete it->getValue();
446 delete PlatformParser;
447 delete GenericParser;
450 void AsmParser::PrintMacroInstantiations() {
451 // Print the active macro instantiation stack.
452 for (std::vector<MacroInstantiation*>::const_reverse_iterator
453 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
454 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
455 "while in macro instantiation");
458 bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
459 if (FatalAssemblerWarnings)
460 return Error(L, Msg, Ranges);
461 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
462 PrintMacroInstantiations();
466 bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
468 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
469 PrintMacroInstantiations();
473 bool AsmParser::EnterIncludeFile(const std::string &Filename) {
474 std::string IncludedFile;
475 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
481 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
486 /// Process the specified .incbin file by seaching for it in the include paths
487 /// then just emitting the byte contents of the file to the streamer. This
488 /// returns true on failure.
489 bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
490 std::string IncludedFile;
491 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
495 // Pick up the bytes from the file and emit them.
496 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
501 void AsmParser::JumpToLoc(SMLoc Loc) {
502 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
503 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
506 const AsmToken &AsmParser::Lex() {
507 const AsmToken *tok = &Lexer.Lex();
509 if (tok->is(AsmToken::Eof)) {
510 // If this is the end of an included file, pop the parent file off the
512 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
513 if (ParentIncludeLoc != SMLoc()) {
514 JumpToLoc(ParentIncludeLoc);
519 if (tok->is(AsmToken::Error))
520 Error(Lexer.getErrLoc(), Lexer.getErr());
525 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
526 // Create the initial section, if requested.
527 if (!NoInitialTextSection)
534 AsmCond StartingCondState = TheCondState;
536 // If we are generating dwarf for assembly source files save the initial text
537 // section and generate a .file directive.
538 if (getContext().getGenDwarfForAssembly()) {
539 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
540 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
541 getStreamer().EmitLabel(SectionStartSym);
542 getContext().setGenDwarfSectionStartSym(SectionStartSym);
543 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
544 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
547 // While we have input, parse each statement.
548 while (Lexer.isNot(AsmToken::Eof)) {
549 if (!ParseStatement()) continue;
551 // We had an error, validate that one was emitted and recover by skipping to
553 assert(HadError && "Parse statement returned an error, but none emitted!");
554 EatToEndOfStatement();
557 if (TheCondState.TheCond != StartingCondState.TheCond ||
558 TheCondState.Ignore != StartingCondState.Ignore)
559 return TokError("unmatched .ifs or .elses");
561 // Check to see there are no empty DwarfFile slots.
562 const std::vector<MCDwarfFile *> &MCDwarfFiles =
563 getContext().getMCDwarfFiles();
564 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
565 if (!MCDwarfFiles[i])
566 TokError("unassigned file number: " + Twine(i) + " for .file directives");
569 // Check to see that all assembler local symbols were actually defined.
570 // Targets that don't do subsections via symbols may not want this, though,
571 // so conservatively exclude them. Only do this if we're finalizing, though,
572 // as otherwise we won't necessarilly have seen everything yet.
573 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
574 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
575 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
578 MCSymbol *Sym = i->getValue();
579 // Variable symbols may not be marked as defined, so check those
580 // explicitly. If we know it's a variable, we have a definition for
581 // the purposes of this check.
582 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
583 // FIXME: We would really like to refer back to where the symbol was
584 // first referenced for a source location. We need to add something
585 // to track that. Currently, we just point to the end of the file.
586 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
587 "assembler local symbol '" + Sym->getName() +
593 // Finalize the output stream if there are no errors and if the client wants
595 if (!HadError && !NoFinalize)
601 void AsmParser::CheckForValidSection() {
602 if (!getStreamer().getCurrentSection()) {
603 TokError("expected section directive before assembly directive");
604 Out.SwitchSection(Ctx.getMachOSection(
606 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
607 0, SectionKind::getText()));
611 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
612 void AsmParser::EatToEndOfStatement() {
613 while (Lexer.isNot(AsmToken::EndOfStatement) &&
614 Lexer.isNot(AsmToken::Eof))
618 if (Lexer.is(AsmToken::EndOfStatement))
622 StringRef AsmParser::ParseStringToEndOfStatement() {
623 const char *Start = getTok().getLoc().getPointer();
625 while (Lexer.isNot(AsmToken::EndOfStatement) &&
626 Lexer.isNot(AsmToken::Eof))
629 const char *End = getTok().getLoc().getPointer();
630 return StringRef(Start, End - Start);
633 StringRef AsmParser::ParseStringToComma() {
634 const char *Start = getTok().getLoc().getPointer();
636 while (Lexer.isNot(AsmToken::EndOfStatement) &&
637 Lexer.isNot(AsmToken::Comma) &&
638 Lexer.isNot(AsmToken::Eof))
641 const char *End = getTok().getLoc().getPointer();
642 return StringRef(Start, End - Start);
645 /// ParseParenExpr - Parse a paren expression and return it.
646 /// NOTE: This assumes the leading '(' has already been consumed.
648 /// parenexpr ::= expr)
650 bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
651 if (ParseExpression(Res)) return true;
652 if (Lexer.isNot(AsmToken::RParen))
653 return TokError("expected ')' in parentheses expression");
654 EndLoc = Lexer.getLoc();
659 /// ParseBracketExpr - Parse a bracket expression and return it.
660 /// NOTE: This assumes the leading '[' has already been consumed.
662 /// bracketexpr ::= expr]
664 bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
665 if (ParseExpression(Res)) return true;
666 if (Lexer.isNot(AsmToken::RBrac))
667 return TokError("expected ']' in brackets expression");
668 EndLoc = Lexer.getLoc();
673 /// ParsePrimaryExpr - Parse a primary expression and return it.
674 /// primaryexpr ::= (parenexpr
675 /// primaryexpr ::= symbol
676 /// primaryexpr ::= number
677 /// primaryexpr ::= '.'
678 /// primaryexpr ::= ~,+,- primaryexpr
679 bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
680 switch (Lexer.getKind()) {
682 return TokError("unknown token in expression");
683 // If we have an error assume that we've already handled it.
684 case AsmToken::Error:
686 case AsmToken::Exclaim:
687 Lex(); // Eat the operator.
688 if (ParsePrimaryExpr(Res, EndLoc))
690 Res = MCUnaryExpr::CreateLNot(Res, getContext());
692 case AsmToken::Dollar:
693 case AsmToken::String:
694 case AsmToken::Identifier: {
695 EndLoc = Lexer.getLoc();
697 StringRef Identifier;
698 if (ParseIdentifier(Identifier))
701 // This is a symbol reference.
702 std::pair<StringRef, StringRef> Split = Identifier.split('@');
703 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
705 // Lookup the symbol variant if used.
706 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
707 if (Split.first.size() != Identifier.size()) {
708 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
709 if (Variant == MCSymbolRefExpr::VK_Invalid) {
710 Variant = MCSymbolRefExpr::VK_None;
711 return TokError("invalid variant '" + Split.second + "'");
715 // If this is an absolute variable reference, substitute it now to preserve
716 // semantics in the face of reassignment.
717 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
719 return Error(EndLoc, "unexpected modifier on variable reference");
721 Res = Sym->getVariableValue();
725 // Otherwise create a symbol ref.
726 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
729 case AsmToken::Integer: {
730 SMLoc Loc = getTok().getLoc();
731 int64_t IntVal = getTok().getIntVal();
732 Res = MCConstantExpr::Create(IntVal, getContext());
733 EndLoc = Lexer.getLoc();
735 // Look for 'b' or 'f' following an Integer as a directional label
736 if (Lexer.getKind() == AsmToken::Identifier) {
737 StringRef IDVal = getTok().getString();
738 if (IDVal == "f" || IDVal == "b"){
739 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
740 IDVal == "f" ? 1 : 0);
741 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
743 if (IDVal == "b" && Sym->isUndefined())
744 return Error(Loc, "invalid reference to undefined symbol");
745 EndLoc = Lexer.getLoc();
746 Lex(); // Eat identifier.
751 case AsmToken::Real: {
752 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
753 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
754 Res = MCConstantExpr::Create(IntVal, getContext());
758 case AsmToken::Dot: {
759 // This is a '.' reference, which references the current PC. Emit a
760 // temporary label to the streamer and refer to it.
761 MCSymbol *Sym = Ctx.CreateTempSymbol();
763 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
764 EndLoc = Lexer.getLoc();
765 Lex(); // Eat identifier.
768 case AsmToken::LParen:
769 Lex(); // Eat the '('.
770 return ParseParenExpr(Res, EndLoc);
771 case AsmToken::LBrac:
772 if (!PlatformParser->HasBracketExpressions())
773 return TokError("brackets expression not supported on this target");
774 Lex(); // Eat the '['.
775 return ParseBracketExpr(Res, EndLoc);
776 case AsmToken::Minus:
777 Lex(); // Eat the operator.
778 if (ParsePrimaryExpr(Res, EndLoc))
780 Res = MCUnaryExpr::CreateMinus(Res, getContext());
783 Lex(); // Eat the operator.
784 if (ParsePrimaryExpr(Res, EndLoc))
786 Res = MCUnaryExpr::CreatePlus(Res, getContext());
788 case AsmToken::Tilde:
789 Lex(); // Eat the operator.
790 if (ParsePrimaryExpr(Res, EndLoc))
792 Res = MCUnaryExpr::CreateNot(Res, getContext());
797 bool AsmParser::ParseExpression(const MCExpr *&Res) {
799 return ParseExpression(Res, EndLoc);
803 AsmParser::ApplyModifierToExpr(const MCExpr *E,
804 MCSymbolRefExpr::VariantKind Variant) {
805 // Recurse over the given expression, rebuilding it to apply the given variant
806 // if there is exactly one symbol.
807 switch (E->getKind()) {
809 case MCExpr::Constant:
812 case MCExpr::SymbolRef: {
813 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
815 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
816 TokError("invalid variant on expression '" +
817 getTok().getIdentifier() + "' (already modified)");
821 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
824 case MCExpr::Unary: {
825 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
826 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
829 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
832 case MCExpr::Binary: {
833 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
834 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
835 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
840 if (!LHS) LHS = BE->getLHS();
841 if (!RHS) RHS = BE->getRHS();
843 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
847 llvm_unreachable("Invalid expression kind!");
850 /// ParseExpression - Parse an expression and return it.
852 /// expr ::= expr &&,|| expr -> lowest.
853 /// expr ::= expr |,^,&,! expr
854 /// expr ::= expr ==,!=,<>,<,<=,>,>= expr
855 /// expr ::= expr <<,>> expr
856 /// expr ::= expr +,- expr
857 /// expr ::= expr *,/,% expr -> highest.
858 /// expr ::= primaryexpr
860 bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
861 // Parse the expression.
863 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
866 // As a special case, we support 'a op b @ modifier' by rewriting the
867 // expression to include the modifier. This is inefficient, but in general we
868 // expect users to use 'a@modifier op b'.
869 if (Lexer.getKind() == AsmToken::At) {
872 if (Lexer.isNot(AsmToken::Identifier))
873 return TokError("unexpected symbol modifier following '@'");
875 MCSymbolRefExpr::VariantKind Variant =
876 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
877 if (Variant == MCSymbolRefExpr::VK_Invalid)
878 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
880 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
882 return TokError("invalid modifier '" + getTok().getIdentifier() +
883 "' (no symbols present)");
890 // Try to constant fold it up front, if possible.
892 if (Res->EvaluateAsAbsolute(Value))
893 Res = MCConstantExpr::Create(Value, getContext());
898 bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
900 return ParseParenExpr(Res, EndLoc) ||
901 ParseBinOpRHS(1, Res, EndLoc);
904 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
907 SMLoc StartLoc = Lexer.getLoc();
908 if (ParseExpression(Expr))
911 if (!Expr->EvaluateAsAbsolute(Res))
912 return Error(StartLoc, "expected absolute expression");
917 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
918 MCBinaryExpr::Opcode &Kind) {
921 return 0; // not a binop.
923 // Lowest Precedence: &&, ||
924 case AsmToken::AmpAmp:
925 Kind = MCBinaryExpr::LAnd;
927 case AsmToken::PipePipe:
928 Kind = MCBinaryExpr::LOr;
932 // Low Precedence: |, &, ^
934 // FIXME: gas seems to support '!' as an infix operator?
936 Kind = MCBinaryExpr::Or;
938 case AsmToken::Caret:
939 Kind = MCBinaryExpr::Xor;
942 Kind = MCBinaryExpr::And;
945 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
946 case AsmToken::EqualEqual:
947 Kind = MCBinaryExpr::EQ;
949 case AsmToken::ExclaimEqual:
950 case AsmToken::LessGreater:
951 Kind = MCBinaryExpr::NE;
954 Kind = MCBinaryExpr::LT;
956 case AsmToken::LessEqual:
957 Kind = MCBinaryExpr::LTE;
959 case AsmToken::Greater:
960 Kind = MCBinaryExpr::GT;
962 case AsmToken::GreaterEqual:
963 Kind = MCBinaryExpr::GTE;
966 // Intermediate Precedence: <<, >>
967 case AsmToken::LessLess:
968 Kind = MCBinaryExpr::Shl;
970 case AsmToken::GreaterGreater:
971 Kind = MCBinaryExpr::Shr;
974 // High Intermediate Precedence: +, -
976 Kind = MCBinaryExpr::Add;
978 case AsmToken::Minus:
979 Kind = MCBinaryExpr::Sub;
982 // Highest Precedence: *, /, %
984 Kind = MCBinaryExpr::Mul;
986 case AsmToken::Slash:
987 Kind = MCBinaryExpr::Div;
989 case AsmToken::Percent:
990 Kind = MCBinaryExpr::Mod;
996 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
997 /// Res contains the LHS of the expression on input.
998 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1001 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
1002 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
1004 // If the next token is lower precedence than we are allowed to eat, return
1005 // successfully with what we ate already.
1006 if (TokPrec < Precedence)
1011 // Eat the next primary expression.
1013 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
1015 // If BinOp binds less tightly with RHS than the operator after RHS, let
1016 // the pending operator take RHS as its LHS.
1017 MCBinaryExpr::Opcode Dummy;
1018 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
1019 if (TokPrec < NextTokPrec) {
1020 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
1023 // Merge LHS and RHS according to operator.
1024 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
1032 /// ::= EndOfStatement
1033 /// ::= Label* Directive ...Operands... EndOfStatement
1034 /// ::= Label* Identifier OperandList* EndOfStatement
1035 bool AsmParser::ParseStatement() {
1036 if (Lexer.is(AsmToken::EndOfStatement)) {
1042 // Statements always start with an identifier or are a full line comment.
1043 AsmToken ID = getTok();
1044 SMLoc IDLoc = ID.getLoc();
1046 int64_t LocalLabelVal = -1;
1047 // A full line comment is a '#' as the first token.
1048 if (Lexer.is(AsmToken::Hash))
1049 return ParseCppHashLineFilenameComment(IDLoc);
1051 // Allow an integer followed by a ':' as a directional local label.
1052 if (Lexer.is(AsmToken::Integer)) {
1053 LocalLabelVal = getTok().getIntVal();
1054 if (LocalLabelVal < 0) {
1055 if (!TheCondState.Ignore)
1056 return TokError("unexpected token at start of statement");
1060 IDVal = getTok().getString();
1061 Lex(); // Consume the integer token to be used as an identifier token.
1062 if (Lexer.getKind() != AsmToken::Colon) {
1063 if (!TheCondState.Ignore)
1064 return TokError("unexpected token at start of statement");
1068 } else if (Lexer.is(AsmToken::Dot)) {
1069 // Treat '.' as a valid identifier in this context.
1073 } else if (ParseIdentifier(IDVal)) {
1074 if (!TheCondState.Ignore)
1075 return TokError("unexpected token at start of statement");
1080 // Handle conditional assembly here before checking for skipping. We
1081 // have to do this so that .endif isn't skipped in a ".if 0" block for
1084 return ParseDirectiveIf(IDLoc);
1085 if (IDVal == ".ifb")
1086 return ParseDirectiveIfb(IDLoc, true);
1087 if (IDVal == ".ifnb")
1088 return ParseDirectiveIfb(IDLoc, false);
1089 if (IDVal == ".ifc")
1090 return ParseDirectiveIfc(IDLoc, true);
1091 if (IDVal == ".ifnc")
1092 return ParseDirectiveIfc(IDLoc, false);
1093 if (IDVal == ".ifdef")
1094 return ParseDirectiveIfdef(IDLoc, true);
1095 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1096 return ParseDirectiveIfdef(IDLoc, false);
1097 if (IDVal == ".elseif")
1098 return ParseDirectiveElseIf(IDLoc);
1099 if (IDVal == ".else")
1100 return ParseDirectiveElse(IDLoc);
1101 if (IDVal == ".endif")
1102 return ParseDirectiveEndIf(IDLoc);
1104 // If we are in a ".if 0" block, ignore this statement.
1105 if (TheCondState.Ignore) {
1106 EatToEndOfStatement();
1110 // FIXME: Recurse on local labels?
1112 // See what kind of statement we have.
1113 switch (Lexer.getKind()) {
1114 case AsmToken::Colon: {
1115 CheckForValidSection();
1117 // identifier ':' -> Label.
1120 // Diagnose attempt to use '.' as a label.
1122 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1124 // Diagnose attempt to use a variable as a label.
1126 // FIXME: Diagnostics. Note the location of the definition as a label.
1127 // FIXME: This doesn't diagnose assignment to a symbol which has been
1128 // implicitly marked as external.
1130 if (LocalLabelVal == -1)
1131 Sym = getContext().GetOrCreateSymbol(IDVal);
1133 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
1134 if (!Sym->isUndefined() || Sym->isVariable())
1135 return Error(IDLoc, "invalid symbol redefinition");
1140 // If we are generating dwarf for assembly source files then gather the
1141 // info to make a dwarf label entry for this label if needed.
1142 if (getContext().getGenDwarfForAssembly())
1143 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1146 // Consume any end of statement token, if present, to avoid spurious
1147 // AddBlankLine calls().
1148 if (Lexer.is(AsmToken::EndOfStatement)) {
1150 if (Lexer.is(AsmToken::Eof))
1154 return ParseStatement();
1157 case AsmToken::Equal:
1158 // identifier '=' ... -> assignment statement
1161 return ParseAssignment(IDVal, true);
1163 default: // Normal instruction or directive.
1167 // If macros are enabled, check to see if this is a macro instantiation.
1169 if (const Macro *M = MacroMap.lookup(IDVal))
1170 return HandleMacroEntry(IDVal, IDLoc, M);
1172 // Otherwise, we have a normal instruction or directive.
1173 if (IDVal[0] == '.' && IDVal != ".") {
1175 // Target hook for parsing target specific directives.
1176 if (!getTargetParser().ParseDirective(ID))
1179 // Assembler features
1180 if (IDVal == ".set" || IDVal == ".equ")
1181 return ParseDirectiveSet(IDVal, true);
1182 if (IDVal == ".equiv")
1183 return ParseDirectiveSet(IDVal, false);
1187 if (IDVal == ".ascii")
1188 return ParseDirectiveAscii(IDVal, false);
1189 if (IDVal == ".asciz" || IDVal == ".string")
1190 return ParseDirectiveAscii(IDVal, true);
1192 if (IDVal == ".byte")
1193 return ParseDirectiveValue(1);
1194 if (IDVal == ".short")
1195 return ParseDirectiveValue(2);
1196 if (IDVal == ".value")
1197 return ParseDirectiveValue(2);
1198 if (IDVal == ".2byte")
1199 return ParseDirectiveValue(2);
1200 if (IDVal == ".long")
1201 return ParseDirectiveValue(4);
1202 if (IDVal == ".int")
1203 return ParseDirectiveValue(4);
1204 if (IDVal == ".4byte")
1205 return ParseDirectiveValue(4);
1206 if (IDVal == ".quad")
1207 return ParseDirectiveValue(8);
1208 if (IDVal == ".8byte")
1209 return ParseDirectiveValue(8);
1210 if (IDVal == ".single" || IDVal == ".float")
1211 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1212 if (IDVal == ".double")
1213 return ParseDirectiveRealValue(APFloat::IEEEdouble);
1215 if (IDVal == ".align") {
1216 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1217 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1219 if (IDVal == ".align32") {
1220 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1221 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1223 if (IDVal == ".balign")
1224 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1225 if (IDVal == ".balignw")
1226 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1227 if (IDVal == ".balignl")
1228 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1229 if (IDVal == ".p2align")
1230 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1231 if (IDVal == ".p2alignw")
1232 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1233 if (IDVal == ".p2alignl")
1234 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1236 if (IDVal == ".org")
1237 return ParseDirectiveOrg();
1239 if (IDVal == ".fill")
1240 return ParseDirectiveFill();
1241 if (IDVal == ".space" || IDVal == ".skip")
1242 return ParseDirectiveSpace();
1243 if (IDVal == ".zero")
1244 return ParseDirectiveZero();
1246 // Symbol attribute directives
1248 if (IDVal == ".extern") {
1249 EatToEndOfStatement(); // .extern is the default, ignore it.
1252 if (IDVal == ".globl" || IDVal == ".global")
1253 return ParseDirectiveSymbolAttribute(MCSA_Global);
1254 if (IDVal == ".indirect_symbol")
1255 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
1256 if (IDVal == ".lazy_reference")
1257 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
1258 if (IDVal == ".no_dead_strip")
1259 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1260 if (IDVal == ".symbol_resolver")
1261 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1262 if (IDVal == ".private_extern")
1263 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1264 if (IDVal == ".reference")
1265 return ParseDirectiveSymbolAttribute(MCSA_Reference);
1266 if (IDVal == ".weak_definition")
1267 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1268 if (IDVal == ".weak_reference")
1269 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
1270 if (IDVal == ".weak_def_can_be_hidden")
1271 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1273 if (IDVal == ".comm" || IDVal == ".common")
1274 return ParseDirectiveComm(/*IsLocal=*/false);
1275 if (IDVal == ".lcomm")
1276 return ParseDirectiveComm(/*IsLocal=*/true);
1278 if (IDVal == ".abort")
1279 return ParseDirectiveAbort();
1280 if (IDVal == ".include")
1281 return ParseDirectiveInclude();
1282 if (IDVal == ".incbin")
1283 return ParseDirectiveIncbin();
1285 if (IDVal == ".code16" || IDVal == ".code16gcc")
1286 return TokError(Twine(IDVal) + " not supported yet");
1288 // Macro-like directives
1289 if (IDVal == ".rept")
1290 return ParseDirectiveRept(IDLoc);
1291 if (IDVal == ".irp")
1292 return ParseDirectiveIrp(IDLoc);
1293 if (IDVal == ".irpc")
1294 return ParseDirectiveIrpc(IDLoc);
1295 if (IDVal == ".endr")
1296 return ParseDirectiveEndr(IDLoc);
1298 // Look up the handler in the handler table.
1299 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1300 DirectiveMap.lookup(IDVal);
1302 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1305 return Error(IDLoc, "unknown directive");
1308 CheckForValidSection();
1310 // Canonicalize the opcode to lower case.
1311 SmallString<128> Opcode;
1312 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1313 Opcode.push_back(tolower(IDVal[i]));
1315 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
1316 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
1319 // Dump the parsed representation, if requested.
1320 if (getShowParsedOperands()) {
1321 SmallString<256> Str;
1322 raw_svector_ostream OS(Str);
1323 OS << "parsed instruction: [";
1324 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1327 ParsedOperands[i]->print(OS);
1331 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
1334 // If we are generating dwarf for assembly source files and the current
1335 // section is the initial text section then generate a .loc directive for
1337 if (!HadError && getContext().getGenDwarfForAssembly() &&
1338 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1339 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1340 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1341 0, DWARF2_LINE_DEFAULT_IS_STMT ?
1342 DWARF2_FLAG_IS_STMT : 0, 0, 0,
1346 // If parsing succeeded, match the instruction.
1348 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1351 // Free any parsed operands.
1352 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1353 delete ParsedOperands[i];
1355 // Don't skip the rest of the line, the instruction parser is responsible for
1360 /// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1361 /// since they may not be able to be tokenized to get to the end of line token.
1362 void AsmParser::EatToEndOfLine() {
1363 if (!Lexer.is(AsmToken::EndOfStatement))
1364 Lexer.LexUntilEndOfLine();
1369 /// ParseCppHashLineFilenameComment as this:
1370 /// ::= # number "filename"
1371 /// or just as a full line comment if it doesn't have a number and a string.
1372 bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1373 Lex(); // Eat the hash token.
1375 if (getLexer().isNot(AsmToken::Integer)) {
1376 // Consume the line since in cases it is not a well-formed line directive,
1377 // as if were simply a full line comment.
1382 int64_t LineNumber = getTok().getIntVal();
1385 if (getLexer().isNot(AsmToken::String)) {
1390 StringRef Filename = getTok().getString();
1391 // Get rid of the enclosing quotes.
1392 Filename = Filename.substr(1, Filename.size()-2);
1394 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1396 CppHashFilename = Filename;
1397 CppHashLineNumber = LineNumber;
1399 // Ignore any trailing characters, they're just comment.
1404 /// DiagHandler - will use the last parsed cpp hash line filename comment
1405 /// for the Filename and LineNo if any in the diagnostic.
1406 void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1407 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1408 raw_ostream &OS = errs();
1410 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1411 const SMLoc &DiagLoc = Diag.getLoc();
1412 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1413 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1415 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1416 // before printing the message.
1417 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1418 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
1419 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1420 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1423 // If we have not parsed a cpp hash line filename comment or the source
1424 // manager changed or buffer changed (like in a nested include) then just
1425 // print the normal diagnostic using its Filename and LineNo.
1426 if (!Parser->CppHashLineNumber ||
1427 &DiagSrcMgr != &Parser->SrcMgr ||
1428 DiagBuf != CppHashBuf) {
1429 if (Parser->SavedDiagHandler)
1430 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1436 // Use the CppHashFilename and calculate a line number based on the
1437 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1439 const std::string Filename = Parser->CppHashFilename;
1441 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1442 int CppHashLocLineNo =
1443 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1444 int LineNo = Parser->CppHashLineNumber - 1 +
1445 (DiagLocLineNo - CppHashLocLineNo);
1447 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1448 Filename, LineNo, Diag.getColumnNo(),
1449 Diag.getKind(), Diag.getMessage(),
1450 Diag.getLineContents(), Diag.getRanges());
1452 if (Parser->SavedDiagHandler)
1453 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1455 NewDiag.print(0, OS);
1458 // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1459 // difference being that that function accepts '@' as part of identifiers and
1460 // we can't do that. AsmLexer.cpp should probably be changed to handle
1461 // '@' as a special case when needed.
1462 static bool isIdentifierChar(char c) {
1463 return isalnum(c) || c == '_' || c == '$' || c == '.';
1466 bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
1467 const MacroParameters &Parameters,
1468 const MacroArguments &A,
1470 unsigned NParameters = Parameters.size();
1471 if (NParameters != 0 && NParameters != A.size())
1472 return Error(L, "Wrong number of arguments");
1474 while (!Body.empty()) {
1475 // Scan for the next substitution.
1476 std::size_t End = Body.size(), Pos = 0;
1477 for (; Pos != End; ++Pos) {
1478 // Check for a substitution or escape.
1480 // This macro has no parameters, look for $0, $1, etc.
1481 if (Body[Pos] != '$' || Pos + 1 == End)
1484 char Next = Body[Pos + 1];
1485 if (Next == '$' || Next == 'n' || isdigit(Next))
1488 // This macro has parameters, look for \foo, \bar, etc.
1489 if (Body[Pos] == '\\' && Pos + 1 != End)
1495 OS << Body.slice(0, Pos);
1497 // Check if we reached the end.
1502 switch (Body[Pos+1]) {
1508 // $n => number of arguments
1513 // $[0-9] => argument
1515 // Missing arguments are ignored.
1516 unsigned Index = Body[Pos+1] - '0';
1517 if (Index >= A.size())
1520 // Otherwise substitute with the token values, with spaces eliminated.
1521 for (MacroArgument::const_iterator it = A[Index].begin(),
1522 ie = A[Index].end(); it != ie; ++it)
1523 OS << it->getString();
1529 unsigned I = Pos + 1;
1530 while (isIdentifierChar(Body[I]) && I + 1 != End)
1533 const char *Begin = Body.data() + Pos +1;
1534 StringRef Argument(Begin, I - (Pos +1));
1536 for (; Index < NParameters; ++Index)
1537 if (Parameters[Index] == Argument)
1540 // FIXME: We should error at the macro definition.
1541 if (Index == NParameters)
1542 return Error(L, "Parameter not found");
1544 for (MacroArgument::const_iterator it = A[Index].begin(),
1545 ie = A[Index].end(); it != ie; ++it)
1546 OS << it->getString();
1548 Pos += 1 + Argument.size();
1550 // Update the scan point.
1551 Body = Body.substr(Pos);
1557 MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1559 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1563 /// ParseMacroArgument - Extract AsmTokens for a macro argument.
1564 /// This is used for both default macro parameter values and the
1565 /// arguments in macro invocations
1566 bool AsmParser::ParseMacroArgument(MacroArgument &MA) {
1567 unsigned ParenLevel = 0;
1570 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
1571 return TokError("unexpected token in macro instantiation");
1573 // HandleMacroEntry relies on not advancing the lexer here
1574 // to be able to fill in the remaining default parameter values
1575 if (Lexer.is(AsmToken::EndOfStatement))
1577 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
1580 // Adjust the current parentheses level.
1581 if (Lexer.is(AsmToken::LParen))
1583 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1586 // Append the token to the current argument list.
1587 MA.push_back(getTok());
1590 if (ParenLevel != 0)
1591 return TokError("unbalanced parentheses in macro argument");
1595 // Parse the macro instantiation arguments.
1596 bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
1597 const unsigned NParameters = M ? M->Parameters.size() : 0;
1599 // Parse two kinds of macro invocations:
1600 // - macros defined without any parameters accept an arbitrary number of them
1601 // - macros defined with parameters accept at most that many of them
1602 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1606 if (ParseMacroArgument(MA))
1611 if (Lexer.is(AsmToken::EndOfStatement))
1614 if (Lexer.is(AsmToken::Comma))
1617 return TokError("Too many arguments");
1620 bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1622 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1623 // this, although we should protect against infinite loops.
1624 if (ActiveMacros.size() == 20)
1625 return TokError("macros cannot be nested more than 20 levels deep");
1628 if (ParseMacroArguments(M, A))
1631 // Remove any trailing empty arguments. Do this after-the-fact as we have
1632 // to keep empty arguments in the middle of the list or positionality
1633 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
1634 while (!A.empty() && A.back().empty())
1637 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1638 // to hold the macro body with substitutions.
1639 SmallString<256> Buf;
1640 StringRef Body = M->Body;
1641 raw_svector_ostream OS(Buf);
1643 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
1646 // We include the .endmacro in the buffer as our queue to exit the macro
1648 OS << ".endmacro\n";
1650 MemoryBuffer *Instantiation =
1651 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
1653 // Create the macro instantiation object and add to the current macro
1654 // instantiation stack.
1655 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
1658 ActiveMacros.push_back(MI);
1660 // Jump to the macro instantiation and prime the lexer.
1661 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1662 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1668 void AsmParser::HandleMacroExit() {
1669 // Jump to the EndOfStatement we should return to, and consume it.
1670 JumpToLoc(ActiveMacros.back()->ExitLoc);
1673 // Pop the instantiation entry.
1674 delete ActiveMacros.back();
1675 ActiveMacros.pop_back();
1678 static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
1679 switch (Value->getKind()) {
1680 case MCExpr::Binary: {
1681 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1682 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
1685 case MCExpr::Target:
1686 case MCExpr::Constant:
1688 case MCExpr::SymbolRef: {
1689 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
1691 return IsUsedIn(Sym, S.getVariableValue());
1695 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1698 llvm_unreachable("Unknown expr kind!");
1701 bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1703 // FIXME: Use better location, we should use proper tokens.
1704 SMLoc EqualLoc = Lexer.getLoc();
1706 const MCExpr *Value;
1707 if (ParseExpression(Value))
1710 // Note: we don't count b as used in "a = b". This is to allow
1714 if (Lexer.isNot(AsmToken::EndOfStatement))
1715 return TokError("unexpected token in assignment");
1717 // Error on assignment to '.'.
1719 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1720 "(use '.space' or '.org').)"));
1723 // Eat the end of statement marker.
1726 // Validate that the LHS is allowed to be a variable (either it has not been
1727 // used as a symbol, or it is an absolute symbol).
1728 MCSymbol *Sym = getContext().LookupSymbol(Name);
1730 // Diagnose assignment to a label.
1732 // FIXME: Diagnostics. Note the location of the definition as a label.
1733 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
1734 if (IsUsedIn(Sym, Value))
1735 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1736 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
1737 ; // Allow redefinitions of undefined symbols only used in directives.
1738 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1739 ; // Allow redefinitions of variables that haven't yet been used.
1740 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
1741 return Error(EqualLoc, "redefinition of '" + Name + "'");
1742 else if (!Sym->isVariable())
1743 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
1744 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
1745 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1748 // Don't count these checks as uses.
1749 Sym->setUsed(false);
1751 Sym = getContext().GetOrCreateSymbol(Name);
1753 // FIXME: Handle '.'.
1755 // Do the assignment.
1756 Out.EmitAssignment(Sym, Value);
1758 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
1764 /// ParseIdentifier:
1767 bool AsmParser::ParseIdentifier(StringRef &Res) {
1768 // The assembler has relaxed rules for accepting identifiers, in particular we
1769 // allow things like '.globl $foo', which would normally be separate
1770 // tokens. At this level, we have already lexed so we cannot (currently)
1771 // handle this as a context dependent token, instead we detect adjacent tokens
1772 // and return the combined identifier.
1773 if (Lexer.is(AsmToken::Dollar)) {
1774 SMLoc DollarLoc = getLexer().getLoc();
1776 // Consume the dollar sign, and check for a following identifier.
1778 if (Lexer.isNot(AsmToken::Identifier))
1781 // We have a '$' followed by an identifier, make sure they are adjacent.
1782 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1785 // Construct the joined identifier and consume the token.
1786 Res = StringRef(DollarLoc.getPointer(),
1787 getTok().getIdentifier().size() + 1);
1792 if (Lexer.isNot(AsmToken::Identifier) &&
1793 Lexer.isNot(AsmToken::String))
1796 Res = getTok().getIdentifier();
1798 Lex(); // Consume the identifier token.
1803 /// ParseDirectiveSet:
1804 /// ::= .equ identifier ',' expression
1805 /// ::= .equiv identifier ',' expression
1806 /// ::= .set identifier ',' expression
1807 bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
1810 if (ParseIdentifier(Name))
1811 return TokError("expected identifier after '" + Twine(IDVal) + "'");
1813 if (getLexer().isNot(AsmToken::Comma))
1814 return TokError("unexpected token in '" + Twine(IDVal) + "'");
1817 return ParseAssignment(Name, allow_redef, true);
1820 bool AsmParser::ParseEscapedString(std::string &Data) {
1821 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
1824 StringRef Str = getTok().getStringContents();
1825 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1826 if (Str[i] != '\\') {
1831 // Recognize escaped characters. Note that this escape semantics currently
1832 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1835 return TokError("unexpected backslash at end of string");
1837 // Recognize octal sequences.
1838 if ((unsigned) (Str[i] - '0') <= 7) {
1839 // Consume up to three octal characters.
1840 unsigned Value = Str[i] - '0';
1842 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1844 Value = Value * 8 + (Str[i] - '0');
1846 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1848 Value = Value * 8 + (Str[i] - '0');
1853 return TokError("invalid octal escape sequence (out of range)");
1855 Data += (unsigned char) Value;
1859 // Otherwise recognize individual escapes.
1862 // Just reject invalid escape sequences for now.
1863 return TokError("invalid escape sequence (unrecognized character)");
1865 case 'b': Data += '\b'; break;
1866 case 'f': Data += '\f'; break;
1867 case 'n': Data += '\n'; break;
1868 case 'r': Data += '\r'; break;
1869 case 't': Data += '\t'; break;
1870 case '"': Data += '"'; break;
1871 case '\\': Data += '\\'; break;
1878 /// ParseDirectiveAscii:
1879 /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1880 bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
1881 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1882 CheckForValidSection();
1885 if (getLexer().isNot(AsmToken::String))
1886 return TokError("expected string in '" + Twine(IDVal) + "' directive");
1889 if (ParseEscapedString(Data))
1892 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
1894 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1898 if (getLexer().is(AsmToken::EndOfStatement))
1901 if (getLexer().isNot(AsmToken::Comma))
1902 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
1911 /// ParseDirectiveValue
1912 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1913 bool AsmParser::ParseDirectiveValue(unsigned Size) {
1914 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1915 CheckForValidSection();
1918 const MCExpr *Value;
1919 SMLoc ExprLoc = getLexer().getLoc();
1920 if (ParseExpression(Value))
1923 // Special case constant expressions to match code generator.
1924 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1925 assert(Size <= 8 && "Invalid size");
1926 uint64_t IntValue = MCE->getValue();
1927 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1928 return Error(ExprLoc, "literal value out of range for directive");
1929 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1931 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
1933 if (getLexer().is(AsmToken::EndOfStatement))
1936 // FIXME: Improve diagnostic.
1937 if (getLexer().isNot(AsmToken::Comma))
1938 return TokError("unexpected token in directive");
1947 /// ParseDirectiveRealValue
1948 /// ::= (.single | .double) [ expression (, expression)* ]
1949 bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1950 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1951 CheckForValidSection();
1954 // We don't truly support arithmetic on floating point expressions, so we
1955 // have to manually parse unary prefixes.
1957 if (getLexer().is(AsmToken::Minus)) {
1960 } else if (getLexer().is(AsmToken::Plus))
1963 if (getLexer().isNot(AsmToken::Integer) &&
1964 getLexer().isNot(AsmToken::Real) &&
1965 getLexer().isNot(AsmToken::Identifier))
1966 return TokError("unexpected token in directive");
1968 // Convert to an APFloat.
1969 APFloat Value(Semantics);
1970 StringRef IDVal = getTok().getString();
1971 if (getLexer().is(AsmToken::Identifier)) {
1972 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1973 Value = APFloat::getInf(Semantics);
1974 else if (!IDVal.compare_lower("nan"))
1975 Value = APFloat::getNaN(Semantics, false, ~0);
1977 return TokError("invalid floating point literal");
1978 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
1979 APFloat::opInvalidOp)
1980 return TokError("invalid floating point literal");
1984 // Consume the numeric token.
1987 // Emit the value as an integer.
1988 APInt AsInt = Value.bitcastToAPInt();
1989 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1990 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1992 if (getLexer().is(AsmToken::EndOfStatement))
1995 if (getLexer().isNot(AsmToken::Comma))
1996 return TokError("unexpected token in directive");
2005 /// ParseDirectiveSpace
2006 /// ::= .space expression [ , expression ]
2007 bool AsmParser::ParseDirectiveSpace() {
2008 CheckForValidSection();
2011 if (ParseAbsoluteExpression(NumBytes))
2014 int64_t FillExpr = 0;
2015 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2016 if (getLexer().isNot(AsmToken::Comma))
2017 return TokError("unexpected token in '.space' directive");
2020 if (ParseAbsoluteExpression(FillExpr))
2023 if (getLexer().isNot(AsmToken::EndOfStatement))
2024 return TokError("unexpected token in '.space' directive");
2030 return TokError("invalid number of bytes in '.space' directive");
2032 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
2033 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
2038 /// ParseDirectiveZero
2039 /// ::= .zero expression
2040 bool AsmParser::ParseDirectiveZero() {
2041 CheckForValidSection();
2044 if (ParseAbsoluteExpression(NumBytes))
2048 if (getLexer().is(AsmToken::Comma)) {
2050 if (ParseAbsoluteExpression(Val))
2054 if (getLexer().isNot(AsmToken::EndOfStatement))
2055 return TokError("unexpected token in '.zero' directive");
2059 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
2064 /// ParseDirectiveFill
2065 /// ::= .fill expression , expression , expression
2066 bool AsmParser::ParseDirectiveFill() {
2067 CheckForValidSection();
2070 if (ParseAbsoluteExpression(NumValues))
2073 if (getLexer().isNot(AsmToken::Comma))
2074 return TokError("unexpected token in '.fill' directive");
2078 if (ParseAbsoluteExpression(FillSize))
2081 if (getLexer().isNot(AsmToken::Comma))
2082 return TokError("unexpected token in '.fill' directive");
2086 if (ParseAbsoluteExpression(FillExpr))
2089 if (getLexer().isNot(AsmToken::EndOfStatement))
2090 return TokError("unexpected token in '.fill' directive");
2094 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2095 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
2097 for (uint64_t i = 0, e = NumValues; i != e; ++i)
2098 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
2103 /// ParseDirectiveOrg
2104 /// ::= .org expression [ , expression ]
2105 bool AsmParser::ParseDirectiveOrg() {
2106 CheckForValidSection();
2108 const MCExpr *Offset;
2109 SMLoc Loc = getTok().getLoc();
2110 if (ParseExpression(Offset))
2113 // Parse optional fill expression.
2114 int64_t FillExpr = 0;
2115 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2116 if (getLexer().isNot(AsmToken::Comma))
2117 return TokError("unexpected token in '.org' directive");
2120 if (ParseAbsoluteExpression(FillExpr))
2123 if (getLexer().isNot(AsmToken::EndOfStatement))
2124 return TokError("unexpected token in '.org' directive");
2129 // Only limited forms of relocatable expressions are accepted here, it
2130 // has to be relative to the current section. The streamer will return
2131 // 'true' if the expression wasn't evaluatable.
2132 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2133 return Error(Loc, "expected assembly-time absolute expression");
2138 /// ParseDirectiveAlign
2139 /// ::= {.align, ...} expression [ , expression [ , expression ]]
2140 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
2141 CheckForValidSection();
2143 SMLoc AlignmentLoc = getLexer().getLoc();
2145 if (ParseAbsoluteExpression(Alignment))
2149 bool HasFillExpr = false;
2150 int64_t FillExpr = 0;
2151 int64_t MaxBytesToFill = 0;
2152 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2153 if (getLexer().isNot(AsmToken::Comma))
2154 return TokError("unexpected token in directive");
2157 // The fill expression can be omitted while specifying a maximum number of
2158 // alignment bytes, e.g:
2160 if (getLexer().isNot(AsmToken::Comma)) {
2162 if (ParseAbsoluteExpression(FillExpr))
2166 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2167 if (getLexer().isNot(AsmToken::Comma))
2168 return TokError("unexpected token in directive");
2171 MaxBytesLoc = getLexer().getLoc();
2172 if (ParseAbsoluteExpression(MaxBytesToFill))
2175 if (getLexer().isNot(AsmToken::EndOfStatement))
2176 return TokError("unexpected token in directive");
2185 // Compute alignment in bytes.
2187 // FIXME: Diagnose overflow.
2188 if (Alignment >= 32) {
2189 Error(AlignmentLoc, "invalid alignment value");
2193 Alignment = 1ULL << Alignment;
2196 // Diagnose non-sensical max bytes to align.
2197 if (MaxBytesLoc.isValid()) {
2198 if (MaxBytesToFill < 1) {
2199 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2200 "many bytes, ignoring maximum bytes expression");
2204 if (MaxBytesToFill >= Alignment) {
2205 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2211 // Check whether we should use optimal code alignment for this .align
2213 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
2214 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2215 ValueSize == 1 && UseCodeAlign) {
2216 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
2218 // FIXME: Target specific behavior about how the "extra" bytes are filled.
2219 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2226 /// ParseDirectiveSymbolAttribute
2227 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
2228 bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
2229 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2232 SMLoc Loc = getTok().getLoc();
2234 if (ParseIdentifier(Name))
2235 return Error(Loc, "expected identifier in directive");
2237 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2239 // Assembler local symbols don't make any sense here. Complain loudly.
2240 if (Sym->isTemporary())
2241 return Error(Loc, "non-local symbol required in directive");
2243 getStreamer().EmitSymbolAttribute(Sym, Attr);
2245 if (getLexer().is(AsmToken::EndOfStatement))
2248 if (getLexer().isNot(AsmToken::Comma))
2249 return TokError("unexpected token in directive");
2258 /// ParseDirectiveComm
2259 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2260 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
2261 CheckForValidSection();
2263 SMLoc IDLoc = getLexer().getLoc();
2265 if (ParseIdentifier(Name))
2266 return TokError("expected identifier in directive");
2268 // Handle the identifier as the key symbol.
2269 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2271 if (getLexer().isNot(AsmToken::Comma))
2272 return TokError("unexpected token in directive");
2276 SMLoc SizeLoc = getLexer().getLoc();
2277 if (ParseAbsoluteExpression(Size))
2280 int64_t Pow2Alignment = 0;
2281 SMLoc Pow2AlignmentLoc;
2282 if (getLexer().is(AsmToken::Comma)) {
2284 Pow2AlignmentLoc = getLexer().getLoc();
2285 if (ParseAbsoluteExpression(Pow2Alignment))
2288 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2289 if (IsLocal && LCOMM == LCOMM::NoAlignment)
2290 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2292 // If this target takes alignments in bytes (not log) validate and convert.
2293 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2294 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
2295 if (!isPowerOf2_64(Pow2Alignment))
2296 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2297 Pow2Alignment = Log2_64(Pow2Alignment);
2301 if (getLexer().isNot(AsmToken::EndOfStatement))
2302 return TokError("unexpected token in '.comm' or '.lcomm' directive");
2306 // NOTE: a size of zero for a .comm should create a undefined symbol
2307 // but a size of .lcomm creates a bss symbol of size zero.
2309 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2310 "be less than zero");
2312 // NOTE: The alignment in the directive is a power of 2 value, the assembler
2313 // may internally end up wanting an alignment in bytes.
2314 // FIXME: Diagnose overflow.
2315 if (Pow2Alignment < 0)
2316 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2317 "alignment, can't be less than zero");
2319 if (!Sym->isUndefined())
2320 return Error(IDLoc, "invalid symbol redefinition");
2322 // Create the Symbol as a common or local common with Size and Pow2Alignment
2324 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
2328 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
2332 /// ParseDirectiveAbort
2333 /// ::= .abort [... message ...]
2334 bool AsmParser::ParseDirectiveAbort() {
2335 // FIXME: Use loc from directive.
2336 SMLoc Loc = getLexer().getLoc();
2338 StringRef Str = ParseStringToEndOfStatement();
2339 if (getLexer().isNot(AsmToken::EndOfStatement))
2340 return TokError("unexpected token in '.abort' directive");
2345 Error(Loc, ".abort detected. Assembly stopping.");
2347 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
2348 // FIXME: Actually abort assembly here.
2353 /// ParseDirectiveInclude
2354 /// ::= .include "filename"
2355 bool AsmParser::ParseDirectiveInclude() {
2356 if (getLexer().isNot(AsmToken::String))
2357 return TokError("expected string in '.include' directive");
2359 std::string Filename = getTok().getString();
2360 SMLoc IncludeLoc = getLexer().getLoc();
2363 if (getLexer().isNot(AsmToken::EndOfStatement))
2364 return TokError("unexpected token in '.include' directive");
2366 // Strip the quotes.
2367 Filename = Filename.substr(1, Filename.size()-2);
2369 // Attempt to switch the lexer to the included file before consuming the end
2370 // of statement to avoid losing it when we switch.
2371 if (EnterIncludeFile(Filename)) {
2372 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
2379 /// ParseDirectiveIncbin
2380 /// ::= .incbin "filename"
2381 bool AsmParser::ParseDirectiveIncbin() {
2382 if (getLexer().isNot(AsmToken::String))
2383 return TokError("expected string in '.incbin' directive");
2385 std::string Filename = getTok().getString();
2386 SMLoc IncbinLoc = getLexer().getLoc();
2389 if (getLexer().isNot(AsmToken::EndOfStatement))
2390 return TokError("unexpected token in '.incbin' directive");
2392 // Strip the quotes.
2393 Filename = Filename.substr(1, Filename.size()-2);
2395 // Attempt to process the included file.
2396 if (ProcessIncbinFile(Filename)) {
2397 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2404 /// ParseDirectiveIf
2405 /// ::= .if expression
2406 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
2407 TheCondStack.push_back(TheCondState);
2408 TheCondState.TheCond = AsmCond::IfCond;
2409 if (TheCondState.Ignore) {
2410 EatToEndOfStatement();
2413 if (ParseAbsoluteExpression(ExprValue))
2416 if (getLexer().isNot(AsmToken::EndOfStatement))
2417 return TokError("unexpected token in '.if' directive");
2421 TheCondState.CondMet = ExprValue;
2422 TheCondState.Ignore = !TheCondState.CondMet;
2428 /// ParseDirectiveIfb
2430 bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2431 TheCondStack.push_back(TheCondState);
2432 TheCondState.TheCond = AsmCond::IfCond;
2434 if (TheCondState.Ignore) {
2435 EatToEndOfStatement();
2437 StringRef Str = ParseStringToEndOfStatement();
2439 if (getLexer().isNot(AsmToken::EndOfStatement))
2440 return TokError("unexpected token in '.ifb' directive");
2444 TheCondState.CondMet = ExpectBlank == Str.empty();
2445 TheCondState.Ignore = !TheCondState.CondMet;
2451 /// ParseDirectiveIfc
2452 /// ::= .ifc string1, string2
2453 bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2454 TheCondStack.push_back(TheCondState);
2455 TheCondState.TheCond = AsmCond::IfCond;
2457 if (TheCondState.Ignore) {
2458 EatToEndOfStatement();
2460 StringRef Str1 = ParseStringToComma();
2462 if (getLexer().isNot(AsmToken::Comma))
2463 return TokError("unexpected token in '.ifc' directive");
2467 StringRef Str2 = ParseStringToEndOfStatement();
2469 if (getLexer().isNot(AsmToken::EndOfStatement))
2470 return TokError("unexpected token in '.ifc' directive");
2474 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2475 TheCondState.Ignore = !TheCondState.CondMet;
2481 /// ParseDirectiveIfdef
2482 /// ::= .ifdef symbol
2483 bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2485 TheCondStack.push_back(TheCondState);
2486 TheCondState.TheCond = AsmCond::IfCond;
2488 if (TheCondState.Ignore) {
2489 EatToEndOfStatement();
2491 if (ParseIdentifier(Name))
2492 return TokError("expected identifier after '.ifdef'");
2496 MCSymbol *Sym = getContext().LookupSymbol(Name);
2499 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2501 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2502 TheCondState.Ignore = !TheCondState.CondMet;
2508 /// ParseDirectiveElseIf
2509 /// ::= .elseif expression
2510 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2511 if (TheCondState.TheCond != AsmCond::IfCond &&
2512 TheCondState.TheCond != AsmCond::ElseIfCond)
2513 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2515 TheCondState.TheCond = AsmCond::ElseIfCond;
2517 bool LastIgnoreState = false;
2518 if (!TheCondStack.empty())
2519 LastIgnoreState = TheCondStack.back().Ignore;
2520 if (LastIgnoreState || TheCondState.CondMet) {
2521 TheCondState.Ignore = true;
2522 EatToEndOfStatement();
2526 if (ParseAbsoluteExpression(ExprValue))
2529 if (getLexer().isNot(AsmToken::EndOfStatement))
2530 return TokError("unexpected token in '.elseif' directive");
2533 TheCondState.CondMet = ExprValue;
2534 TheCondState.Ignore = !TheCondState.CondMet;
2540 /// ParseDirectiveElse
2542 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
2543 if (getLexer().isNot(AsmToken::EndOfStatement))
2544 return TokError("unexpected token in '.else' directive");
2548 if (TheCondState.TheCond != AsmCond::IfCond &&
2549 TheCondState.TheCond != AsmCond::ElseIfCond)
2550 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2552 TheCondState.TheCond = AsmCond::ElseCond;
2553 bool LastIgnoreState = false;
2554 if (!TheCondStack.empty())
2555 LastIgnoreState = TheCondStack.back().Ignore;
2556 if (LastIgnoreState || TheCondState.CondMet)
2557 TheCondState.Ignore = true;
2559 TheCondState.Ignore = false;
2564 /// ParseDirectiveEndIf
2566 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
2567 if (getLexer().isNot(AsmToken::EndOfStatement))
2568 return TokError("unexpected token in '.endif' directive");
2572 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2573 TheCondStack.empty())
2574 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2576 if (!TheCondStack.empty()) {
2577 TheCondState = TheCondStack.back();
2578 TheCondStack.pop_back();
2584 /// ParseDirectiveFile
2585 /// ::= .file [number] filename
2586 /// ::= .file number directory filename
2587 bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
2588 // FIXME: I'm not sure what this is.
2589 int64_t FileNumber = -1;
2590 SMLoc FileNumberLoc = getLexer().getLoc();
2591 if (getLexer().is(AsmToken::Integer)) {
2592 FileNumber = getTok().getIntVal();
2596 return TokError("file number less than one");
2599 if (getLexer().isNot(AsmToken::String))
2600 return TokError("unexpected token in '.file' directive");
2602 // Usually the directory and filename together, otherwise just the directory.
2603 StringRef Path = getTok().getString();
2604 Path = Path.substr(1, Path.size()-2);
2607 StringRef Directory;
2609 if (getLexer().is(AsmToken::String)) {
2610 if (FileNumber == -1)
2611 return TokError("explicit path specified, but no file number");
2612 Filename = getTok().getString();
2613 Filename = Filename.substr(1, Filename.size()-2);
2620 if (getLexer().isNot(AsmToken::EndOfStatement))
2621 return TokError("unexpected token in '.file' directive");
2623 if (FileNumber == -1)
2624 getStreamer().EmitFileDirective(Filename);
2626 if (getContext().getGenDwarfForAssembly() == true)
2627 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2628 "used to generate dwarf debug info for assembly code");
2630 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2631 Error(FileNumberLoc, "file number already allocated");
2637 /// ParseDirectiveLine
2638 /// ::= .line [number]
2639 bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
2640 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2641 if (getLexer().isNot(AsmToken::Integer))
2642 return TokError("unexpected token in '.line' directive");
2644 int64_t LineNumber = getTok().getIntVal();
2648 // FIXME: Do something with the .line.
2651 if (getLexer().isNot(AsmToken::EndOfStatement))
2652 return TokError("unexpected token in '.line' directive");
2658 /// ParseDirectiveLoc
2659 /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2660 /// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2661 /// The first number is a file number, must have been previously assigned with
2662 /// a .file directive, the second number is the line number and optionally the
2663 /// third number is a column position (zero if not specified). The remaining
2664 /// optional items are .loc sub-directives.
2665 bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
2667 if (getLexer().isNot(AsmToken::Integer))
2668 return TokError("unexpected token in '.loc' directive");
2669 int64_t FileNumber = getTok().getIntVal();
2671 return TokError("file number less than one in '.loc' directive");
2672 if (!getContext().isValidDwarfFileNumber(FileNumber))
2673 return TokError("unassigned file number in '.loc' directive");
2676 int64_t LineNumber = 0;
2677 if (getLexer().is(AsmToken::Integer)) {
2678 LineNumber = getTok().getIntVal();
2680 return TokError("line number less than one in '.loc' directive");
2684 int64_t ColumnPos = 0;
2685 if (getLexer().is(AsmToken::Integer)) {
2686 ColumnPos = getTok().getIntVal();
2688 return TokError("column position less than zero in '.loc' directive");
2692 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2694 int64_t Discriminator = 0;
2695 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2697 if (getLexer().is(AsmToken::EndOfStatement))
2701 SMLoc Loc = getTok().getLoc();
2702 if (getParser().ParseIdentifier(Name))
2703 return TokError("unexpected token in '.loc' directive");
2705 if (Name == "basic_block")
2706 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2707 else if (Name == "prologue_end")
2708 Flags |= DWARF2_FLAG_PROLOGUE_END;
2709 else if (Name == "epilogue_begin")
2710 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2711 else if (Name == "is_stmt") {
2712 SMLoc Loc = getTok().getLoc();
2713 const MCExpr *Value;
2714 if (getParser().ParseExpression(Value))
2716 // The expression must be the constant 0 or 1.
2717 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2718 int Value = MCE->getValue();
2720 Flags &= ~DWARF2_FLAG_IS_STMT;
2721 else if (Value == 1)
2722 Flags |= DWARF2_FLAG_IS_STMT;
2724 return Error(Loc, "is_stmt value not 0 or 1");
2727 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2730 else if (Name == "isa") {
2731 SMLoc Loc = getTok().getLoc();
2732 const MCExpr *Value;
2733 if (getParser().ParseExpression(Value))
2735 // The expression must be a constant greater or equal to 0.
2736 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2737 int Value = MCE->getValue();
2739 return Error(Loc, "isa number less than zero");
2743 return Error(Loc, "isa number not a constant value");
2746 else if (Name == "discriminator") {
2747 if (getParser().ParseAbsoluteExpression(Discriminator))
2751 return Error(Loc, "unknown sub-directive in '.loc' directive");
2754 if (getLexer().is(AsmToken::EndOfStatement))
2759 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2760 Isa, Discriminator, StringRef());
2765 /// ParseDirectiveStabs
2766 /// ::= .stabs string, number, number, number
2767 bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2768 SMLoc DirectiveLoc) {
2769 return TokError("unsupported directive '" + Directive + "'");
2772 /// ParseDirectiveCFISections
2773 /// ::= .cfi_sections section [, section]
2774 bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2775 SMLoc DirectiveLoc) {
2780 if (getParser().ParseIdentifier(Name))
2781 return TokError("Expected an identifier");
2783 if (Name == ".eh_frame")
2785 else if (Name == ".debug_frame")
2788 if (getLexer().is(AsmToken::Comma)) {
2791 if (getParser().ParseIdentifier(Name))
2792 return TokError("Expected an identifier");
2794 if (Name == ".eh_frame")
2796 else if (Name == ".debug_frame")
2800 getStreamer().EmitCFISections(EH, Debug);
2805 /// ParseDirectiveCFIStartProc
2806 /// ::= .cfi_startproc
2807 bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2808 SMLoc DirectiveLoc) {
2809 getStreamer().EmitCFIStartProc();
2813 /// ParseDirectiveCFIEndProc
2814 /// ::= .cfi_endproc
2815 bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
2816 getStreamer().EmitCFIEndProc();
2820 /// ParseRegisterOrRegisterNumber - parse register name or number.
2821 bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2822 SMLoc DirectiveLoc) {
2825 if (getLexer().isNot(AsmToken::Integer)) {
2826 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2829 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
2831 return getParser().ParseAbsoluteExpression(Register);
2836 /// ParseDirectiveCFIDefCfa
2837 /// ::= .cfi_def_cfa register, offset
2838 bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2839 SMLoc DirectiveLoc) {
2840 int64_t Register = 0;
2841 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2844 if (getLexer().isNot(AsmToken::Comma))
2845 return TokError("unexpected token in directive");
2849 if (getParser().ParseAbsoluteExpression(Offset))
2852 getStreamer().EmitCFIDefCfa(Register, Offset);
2856 /// ParseDirectiveCFIDefCfaOffset
2857 /// ::= .cfi_def_cfa_offset offset
2858 bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2859 SMLoc DirectiveLoc) {
2861 if (getParser().ParseAbsoluteExpression(Offset))
2864 getStreamer().EmitCFIDefCfaOffset(Offset);
2868 /// ParseDirectiveCFIAdjustCfaOffset
2869 /// ::= .cfi_adjust_cfa_offset adjustment
2870 bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2871 SMLoc DirectiveLoc) {
2872 int64_t Adjustment = 0;
2873 if (getParser().ParseAbsoluteExpression(Adjustment))
2876 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2880 /// ParseDirectiveCFIDefCfaRegister
2881 /// ::= .cfi_def_cfa_register register
2882 bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2883 SMLoc DirectiveLoc) {
2884 int64_t Register = 0;
2885 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2888 getStreamer().EmitCFIDefCfaRegister(Register);
2892 /// ParseDirectiveCFIOffset
2893 /// ::= .cfi_offset register, offset
2894 bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2895 int64_t Register = 0;
2898 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2901 if (getLexer().isNot(AsmToken::Comma))
2902 return TokError("unexpected token in directive");
2905 if (getParser().ParseAbsoluteExpression(Offset))
2908 getStreamer().EmitCFIOffset(Register, Offset);
2912 /// ParseDirectiveCFIRelOffset
2913 /// ::= .cfi_rel_offset register, offset
2914 bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2915 SMLoc DirectiveLoc) {
2916 int64_t Register = 0;
2918 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2921 if (getLexer().isNot(AsmToken::Comma))
2922 return TokError("unexpected token in directive");
2926 if (getParser().ParseAbsoluteExpression(Offset))
2929 getStreamer().EmitCFIRelOffset(Register, Offset);
2933 static bool isValidEncoding(int64_t Encoding) {
2934 if (Encoding & ~0xff)
2937 if (Encoding == dwarf::DW_EH_PE_omit)
2940 const unsigned Format = Encoding & 0xf;
2941 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2942 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2943 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2944 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2947 const unsigned Application = Encoding & 0x70;
2948 if (Application != dwarf::DW_EH_PE_absptr &&
2949 Application != dwarf::DW_EH_PE_pcrel)
2955 /// ParseDirectiveCFIPersonalityOrLsda
2956 /// ::= .cfi_personality encoding, [symbol_name]
2957 /// ::= .cfi_lsda encoding, [symbol_name]
2958 bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
2959 SMLoc DirectiveLoc) {
2960 int64_t Encoding = 0;
2961 if (getParser().ParseAbsoluteExpression(Encoding))
2963 if (Encoding == dwarf::DW_EH_PE_omit)
2966 if (!isValidEncoding(Encoding))
2967 return TokError("unsupported encoding.");
2969 if (getLexer().isNot(AsmToken::Comma))
2970 return TokError("unexpected token in directive");
2974 if (getParser().ParseIdentifier(Name))
2975 return TokError("expected identifier in directive");
2977 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2979 if (IDVal == ".cfi_personality")
2980 getStreamer().EmitCFIPersonality(Sym, Encoding);
2982 assert(IDVal == ".cfi_lsda");
2983 getStreamer().EmitCFILsda(Sym, Encoding);
2988 /// ParseDirectiveCFIRememberState
2989 /// ::= .cfi_remember_state
2990 bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2991 SMLoc DirectiveLoc) {
2992 getStreamer().EmitCFIRememberState();
2996 /// ParseDirectiveCFIRestoreState
2997 /// ::= .cfi_remember_state
2998 bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2999 SMLoc DirectiveLoc) {
3000 getStreamer().EmitCFIRestoreState();
3004 /// ParseDirectiveCFISameValue
3005 /// ::= .cfi_same_value register
3006 bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3007 SMLoc DirectiveLoc) {
3008 int64_t Register = 0;
3010 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3013 getStreamer().EmitCFISameValue(Register);
3018 /// ParseDirectiveCFIRestore
3019 /// ::= .cfi_restore register
3020 bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
3021 SMLoc DirectiveLoc) {
3022 int64_t Register = 0;
3023 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3026 getStreamer().EmitCFIRestore(Register);
3031 /// ParseDirectiveCFIEscape
3032 /// ::= .cfi_escape expression[,...]
3033 bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
3034 SMLoc DirectiveLoc) {
3037 if (getParser().ParseAbsoluteExpression(CurrValue))
3040 Values.push_back((uint8_t)CurrValue);
3042 while (getLexer().is(AsmToken::Comma)) {
3045 if (getParser().ParseAbsoluteExpression(CurrValue))
3048 Values.push_back((uint8_t)CurrValue);
3051 getStreamer().EmitCFIEscape(Values);
3055 /// ParseDirectiveCFISignalFrame
3056 /// ::= .cfi_signal_frame
3057 bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3058 SMLoc DirectiveLoc) {
3059 if (getLexer().isNot(AsmToken::EndOfStatement))
3060 return Error(getLexer().getLoc(),
3061 "unexpected token in '" + Directive + "' directive");
3063 getStreamer().EmitCFISignalFrame();
3068 /// ParseDirectiveMacrosOnOff
3071 bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3072 SMLoc DirectiveLoc) {
3073 if (getLexer().isNot(AsmToken::EndOfStatement))
3074 return Error(getLexer().getLoc(),
3075 "unexpected token in '" + Directive + "' directive");
3077 getParser().MacrosEnabled = Directive == ".macros_on";
3082 /// ParseDirectiveMacro
3083 /// ::= .macro name [parameters]
3084 bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3085 SMLoc DirectiveLoc) {
3087 if (getParser().ParseIdentifier(Name))
3088 return TokError("expected identifier in '.macro' directive");
3090 MacroParameters Parameters;
3091 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3093 MacroParameter Parameter;
3094 if (getParser().ParseIdentifier(Parameter))
3095 return TokError("expected identifier in '.macro' directive");
3096 Parameters.push_back(Parameter);
3098 if (getLexer().isNot(AsmToken::Comma))
3104 if (getLexer().isNot(AsmToken::EndOfStatement))
3105 return TokError("unexpected token in '.macro' directive");
3107 // Eat the end of statement.
3110 AsmToken EndToken, StartToken = getTok();
3112 // Lex the macro definition.
3114 // Check whether we have reached the end of the file.
3115 if (getLexer().is(AsmToken::Eof))
3116 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3118 // Otherwise, check whether we have reach the .endmacro.
3119 if (getLexer().is(AsmToken::Identifier) &&
3120 (getTok().getIdentifier() == ".endm" ||
3121 getTok().getIdentifier() == ".endmacro")) {
3122 EndToken = getTok();
3124 if (getLexer().isNot(AsmToken::EndOfStatement))
3125 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3130 // Otherwise, scan til the end of the statement.
3131 getParser().EatToEndOfStatement();
3134 if (getParser().MacroMap.lookup(Name)) {
3135 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3138 const char *BodyStart = StartToken.getLoc().getPointer();
3139 const char *BodyEnd = EndToken.getLoc().getPointer();
3140 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3141 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
3145 /// ParseDirectiveEndMacro
3148 bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
3149 SMLoc DirectiveLoc) {
3150 if (getLexer().isNot(AsmToken::EndOfStatement))
3151 return TokError("unexpected token in '" + Directive + "' directive");
3153 // If we are inside a macro instantiation, terminate the current
3155 if (!getParser().ActiveMacros.empty()) {
3156 getParser().HandleMacroExit();
3160 // Otherwise, this .endmacro is a stray entry in the file; well formed
3161 // .endmacro directives are handled during the macro definition parsing.
3162 return TokError("unexpected '" + Directive + "' in file, "
3163 "no current macro definition");
3166 /// ParseDirectivePurgeMacro
3168 bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3169 SMLoc DirectiveLoc) {
3171 if (getParser().ParseIdentifier(Name))
3172 return TokError("expected identifier in '.purgem' directive");
3174 if (getLexer().isNot(AsmToken::EndOfStatement))
3175 return TokError("unexpected token in '.purgem' directive");
3177 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3178 if (I == getParser().MacroMap.end())
3179 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3181 // Undefine the macro.
3182 delete I->getValue();
3183 getParser().MacroMap.erase(I);
3187 bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
3188 getParser().CheckForValidSection();
3190 const MCExpr *Value;
3192 if (getParser().ParseExpression(Value))
3195 if (getLexer().isNot(AsmToken::EndOfStatement))
3196 return TokError("unexpected token in directive");
3198 if (DirName[1] == 's')
3199 getStreamer().EmitSLEB128Value(Value);
3201 getStreamer().EmitULEB128Value(Value);
3206 Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
3207 AsmToken EndToken, StartToken = getTok();
3209 unsigned NestLevel = 0;
3211 // Check whether we have reached the end of the file.
3212 if (getLexer().is(AsmToken::Eof)) {
3213 Error(DirectiveLoc, "no matching '.endr' in definition");
3217 if (Lexer.is(AsmToken::Identifier) &&
3218 (getTok().getIdentifier() == ".rept")) {
3222 // Otherwise, check whether we have reached the .endr.
3223 if (Lexer.is(AsmToken::Identifier) &&
3224 getTok().getIdentifier() == ".endr") {
3225 if (NestLevel == 0) {
3226 EndToken = getTok();
3228 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3229 TokError("unexpected token in '.endr' directive");
3237 // Otherwise, scan till the end of the statement.
3238 EatToEndOfStatement();
3241 const char *BodyStart = StartToken.getLoc().getPointer();
3242 const char *BodyEnd = EndToken.getLoc().getPointer();
3243 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3245 // We Are Anonymous.
3247 MacroParameters Parameters;
3248 return new Macro(Name, Body, Parameters);
3251 void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3252 raw_svector_ostream &OS) {
3255 MemoryBuffer *Instantiation =
3256 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3258 // Create the macro instantiation object and add to the current macro
3259 // instantiation stack.
3260 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
3263 ActiveMacros.push_back(MI);
3265 // Jump to the macro instantiation and prime the lexer.
3266 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3267 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3271 bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3273 if (ParseAbsoluteExpression(Count))
3274 return TokError("unexpected token in '.rept' directive");
3277 return TokError("Count is negative");
3279 if (Lexer.isNot(AsmToken::EndOfStatement))
3280 return TokError("unexpected token in '.rept' directive");
3282 // Eat the end of statement.
3285 // Lex the rept definition.
3286 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3290 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3291 // to hold the macro body with substitutions.
3292 SmallString<256> Buf;
3293 MacroParameters Parameters;
3295 raw_svector_ostream OS(Buf);
3297 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3300 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3305 /// ParseDirectiveIrp
3306 /// ::= .irp symbol,values
3307 bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
3308 MacroParameters Parameters;
3309 MacroParameter Parameter;
3311 if (ParseIdentifier(Parameter))
3312 return TokError("expected identifier in '.irp' directive");
3314 Parameters.push_back(Parameter);
3316 if (Lexer.isNot(AsmToken::Comma))
3317 return TokError("expected comma in '.irp' directive");
3322 if (ParseMacroArguments(0, A))
3325 // Eat the end of statement.
3328 // Lex the irp definition.
3329 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3333 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3334 // to hold the macro body with substitutions.
3335 SmallString<256> Buf;
3336 raw_svector_ostream OS(Buf);
3338 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3339 MacroArguments Args;
3342 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3346 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3351 /// ParseDirectiveIrpc
3352 /// ::= .irpc symbol,values
3353 bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
3354 MacroParameters Parameters;
3355 MacroParameter Parameter;
3357 if (ParseIdentifier(Parameter))
3358 return TokError("expected identifier in '.irpc' directive");
3360 Parameters.push_back(Parameter);
3362 if (Lexer.isNot(AsmToken::Comma))
3363 return TokError("expected comma in '.irpc' directive");
3368 if (ParseMacroArguments(0, A))
3371 if (A.size() != 1 || A.front().size() != 1)
3372 return TokError("unexpected token in '.irpc' directive");
3374 // Eat the end of statement.
3377 // Lex the irpc definition.
3378 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3382 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3383 // to hold the macro body with substitutions.
3384 SmallString<256> Buf;
3385 raw_svector_ostream OS(Buf);
3387 StringRef Values = A.front().front().getString();
3388 std::size_t I, End = Values.size();
3389 for (I = 0; I < End; ++I) {
3391 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3393 MacroArguments Args;
3394 Args.push_back(Arg);
3396 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3400 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3405 bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3406 if (ActiveMacros.empty())
3407 return TokError("unexpected '.endr' directive, no current .rept");
3409 // The only .repl that should get here are the ones created by
3410 // InstantiateMacroLikeBody.
3411 assert(getLexer().is(AsmToken::EndOfStatement));
3417 /// \brief Create an MCAsmParser instance.
3418 MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
3419 MCContext &C, MCStreamer &Out,
3420 const MCAsmInfo &MAI) {
3421 return new AsmParser(SM, C, Out, MAI);